3

I have a question about using of Task.

I write the follow code:

static void Main(string[] args)
{
    Task t = new Task(() => DoWork());
}

private static void DoWork()
{
    //Do Something
}

DoWork is run on thread from ThreadPool. for the example let assume the thread num is 3.

My Question: There is option that in middle of the func DoWork, because of context switch etc', the continuous of the function will be in thread that is not 3?

Thank!

maz
  • 515
  • 2
  • 8
  • 22
  • Are you asking will the thread switch mid processing to a different thread? – Liam Nov 14 '16 at 12:00
  • 3
    No, context switch doesn't mean it goes to a different thread, just that processor takes up a different thread / logic to process, this one still goes on the same thread – Mrinal Kamboj Nov 14 '16 at 12:00
  • Possible duplicate of [thread context switch vs process context switch](http://stackoverflow.com/questions/5440128/thread-context-switch-vs-process-context-switch) – Liam Nov 14 '16 at 12:03
  • @MrinalKamboj if the function `DoWork` contain some waiting. The thread pool can use DoWork thread for another task? – maz Nov 14 '16 at 13:43
  • @maz, that's not how the thread pool works, a thread once assigned a work it will only complete that task, no other thread will come in the picture for same work. All the work data gets stored in TLS as context switching happens. Benefit of Threadpool is we don't have to manage individual threads. We just queue the task, Threadpool will have a mechanism based on various factors like system configuration and duration of work, to understand number of threads required to process and remaining wait in queue. This is different for `Async-Await`, which free up calling thread. – Mrinal Kamboj Nov 14 '16 at 17:51
  • Possible duplicate of [Can the TPL run the Task on more than one thread?](https://stackoverflow.com/questions/24959585/can-the-tpl-run-the-task-on-more-than-one-thread) – yonisha Jun 17 '17 at 18:18

2 Answers2

4

My Question: There is option that in middle of the func DoWork, because of context switch etc', the continuous of the function will be in thread that is not 3?

No, context switch doesn't mean it goes to a different thread, just that processor core takes up a different thread / logic to process, this one still goes on the same thread. Most of the threads have same priority until and unless scheduled by CLR like GC or OS, which will have priority over other threads and freeze all other threads. Rest all have to keep on leaving processor core after some logical processing or I must say Processor core (Thread scheduling logic) keep scheduling other threads, thus not starving any of them. Please note at a given time, processor core can only schedule one thread / unit of work

Mrinal Kamboj
  • 11,300
  • 5
  • 40
  • 74
4

My Question: There is option that in middle of the func DoWork, because of context switch etc', the continuous of the function will be in thread that is not 3?

No. From the moment a thread from the thread pool would be assigned to run the code of DoWork, this thread would execute the code and when the the thread has completed its work would back to the queues that contains other work that should be run to fetch the next one, if there is any at all.

Regarding the context switch, it doesn't happen in a thread but between two threads. Each core in a CPU can execute only one thread at a time for a time slice. You have a context switch, when one thread's work has not been completed in the given time slice. So it has to be paused and another thread being executed for it's time slice. Then the thread scheduler would schedule the paused thread to run at another time.

Christos
  • 53,228
  • 8
  • 76
  • 108
  • if the function DoWork contain some waiting. The thread pool can use DoWork thread for another task? – maz Nov 14 '16 at 12:42
  • @maz as far as I am aware of nope. It's not like the threads of asp.net, where when they start to process a web request, if the code is asynchronous and there is an await for an IO the thread goes back to serve other web requests and when the IO would be completed the continuation would be executed on another thread, in order the web request to be fully processed. – Christos Nov 14 '16 at 19:09