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 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: