C# Is a Parallel foreach while loop possible?
I'm not sure how the internal thread handling of Parallel.foreach works and whether it can guarantee that a construct like this would work?
Parallel.Foreach(Connections, c =>
{
Input in = c.getInput();
while (in.hasNext()) {
object o = in.getNext();
dosomething(o);
}
}
);
where in.hasNext() just waits for an object in the input stream and returns true. Basically can I run a bunch of infinite while loops in a parallel foreach structure whilst guaranteeing that they will all run at the same time.
(For the brave, can I adjust this so I can edit the connection list by adding (and removing, which should be trivial) connections and it will still read 开发者_Go百科input from all connections in the list).
The number of threads using it is limited, so some elements will be processed one after another.
Editing lists while enumerating is not good. You will likely get an exception (depends on the list you're using
Why not start a new thread per connection?
Example code:
public ConnectionOpen(data)
{
Connection conn=new ...
lock(Connections)
{
Connections.Add(conn);
}
new Thread(()=>
{
Receive(conn);//infinite loop goes here
}).Start();
}
public ConnectionClose(conn)
{
bool removed=false;
lock(Connections)
{
removed=Connections.Remove(conn);
}
if(removed) conn.StopReceiving();
}
Basically can I run a bunch of infinite while loops in a parallel foreach structure whilst guaranteeing that they will all run at the same time
No. Only a limited number of worker threads will be started concurrently, so if you run more infinite loops than there are threads, the last ones will never run...
Technically, this code will work, but the precise number of threads running "simulataneously" will vary.
In addition to the parallel extensions, .NET 4 also added 'hill climbing'and thread injection to the thread pool, so basically the .NET threadpool will try adding threads to the Parallel.ForEach loop to see if more threads get completed, since yours will never complete the number of threads will vary, but I'm guessing will be non-ideal.
You might try using a ParallelWhile construct, the team has blogged about a couple ways to do this, here is one.
精彩评论