How can I ensure two tasks are running on different threads or even different processors with the new Task Parallel Library?

If "network stream" means the NetworkStream class, I'm not sure why you really need two separate threads here. Are you basing that on the following quote from the documentation.

If "network stream" means the NetworkStream class, I'm not sure why you really need two separate threads here. Are you basing that on the following quote from the documentation: "As long as there is one unique thread for the write operations and one unique thread for the read operations, there will be no cross-interference between read and write threads and no synchronization is required" If so, that's only a suggestion - they're saying that if you do things this way, you don't need to do any synchronization. The main reason they're saying this is that NetworkStream is unusual in that it supports access from two threads simultaneously as long as one is reading and one is writing.

But it's clear from the documentation that you also have the option to perform synchronization - they're just saying that synchronization is unnecessary if you choose to go down the path of having exactly one read thread, and exactly one write thread. But if for some reason you really do need two dedicated threads, then...I'd just use the Thread class. Trying to get Task to do that for you (either directly, or indirectly via a TaskScheduler) is probably just making things more complex.

This doesn't prevent you from using the TPL - you could use the TaskCompletionSource to create tasks, and finish them at your own leisure. (TCS is the normal solution for situations where you want to control how tasks execute, but still expose them as a Task object. ).

I need to both read and write at the same time because I am streaming. So if I am getting it right I should use the tasks only for executing tasks inside the thread, right? – the_drow Dec 21 '10 at 8:49 I wasn't questioning whether you need to read and write simultaneously.

I was questioning the need for (specifically) two threads. You might have any number of threads - as long as no more than 1 reads at any one time and no more than 1 writes at any one time, you don't need to dedicate one thread to reading and one thread to writing. There are a couple of reasons to use Task: a) use a built-in Task if its execution model suits you - not the case here - or b) implement your own Task if you have an unusual execution model, but still want Task semantics.

B) might suit you here, perhaps. – Ian Griffiths Jan 5 at 1:24.

LongRunning is a hint that it should be executed on a new Thread, instead of an existing thread in the ThreadPool. This is so you won't exhaust the ThreadPool with long-running tasks. There are no guarantees that two different threads will be used, neither in TPL, nor with the ThreadPool.

For example, it could be the same thread executing both tasks synchronously. Have you looked into using asynchronous io instead?

Do you mean using BeginXXX and EndXX yes but that also can complete synchronously. – the_drow Dec 18 '10 at 8:04.

String forums = File. ReadAllLines(Environment. CurrentDirectory + @"\forums.

Txt"); Task tasks = new Taskforums. Length ; int ctr = 0; DateTime Start = DateTime. Now;` foreach(string s in forums)` { object state = s; var task = Task.Factory.

StartNew(() => DoSomeWork(state),TaskCreationOptions. LongRunning); tasksctr = task; ctr++; } Task. WaitAll(tasks); DateTime end = DateTime.

Now; TimeSpan elapsed = end - Start; string totalMs = elapsed. TotalMilliseconds.ToString(); Console. WriteLine("DONE in " +totalMs + " ms.

Any Key to quit. "); Console.ReadKey().

That won't guarantee my tasks to run on different threads – the_drow Dec 18 '10 at 8:05.

The LongRunning enum method overload tells the Task library to dedicate a thread to each task, regardless of the number of cores it can use.

I know that, but it's just a suggestion. Isn't it> – the_drow Dec 20 '10 at 13:34.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions