views:

294

answers:

3

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).

A: 

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

Thomas Levesque
A: 
  1. The number of threads used it limited, so some elements will be processed each fater another.

  2. Editing lists while enumerating is not good. You will likely get an exception (depends on the list you're using

  3. Why not start a new thread per connection?

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();
}

Floste
I tried to format that code a little better, but for some reason the StackO parser was having trouble with it.
Robaticus
A: 

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.

Rick
The parallel while loop runs the do block of the while in parallel.I'm trying to get a parallel for loop with an infinite loop in the do block, so that each part of the for loop gets run even though the its an infinite loop
Raynos
I don't think you will get this behaviour with Parallel.ForEach, it sounds like you're looking to have N threads run at the same time where N is large.Why exactly do you need so many threads running at once?
Rick