1

I'm trying to understand how threading works when using a single core, not hyper-threaded processor.

I have two identical console applications running on a machine that has one core CPU and 1 GB of memory.

In this console application, I have a method that starts a new Thread(). I know that when we Start() a new thread, and Join() the same thread, it blocks the main thread that calls it.

My question is, if I have two console applications doing this, will one block the other?

If so, and in the case that I have a Thread.Sleep(10000) on this console app, in this time, if this sleep is running on one console, will the other freeze?

1
1

Starting a new thread, by itself, does not cause the current thread to sleep or freeze. The OS schedules these threads (alongside many other threads) to take turn executing.

If the first application starts the second application as a process, via System.Diagnostics.Process, this also does not cause the current process or thread to sleep or freeze. The threads that run in these two applications (processes) will also take turn executing.

In other words, if anything ever sleeps or freezes, it is either due to your code, or is due to "blocking I/O" in which some input/output operations were performed in a way that doesn't resume execution until that operation is completed. Blocking I/O will only block the thread on which it is performed.

If an application is given OS administrative privilege, it is possible to issue some special OS command to forcibly freeze another process or thread. This is an advanced topic and is typically not encountered in typical software development.

To get a view of processes and threads, you can try the "SysInternals Process Explorer" utility. However, I must warn you that if you misuse this utility, (e.g. randomly terminating applications or threads) it can cause abnormal software operations, data corruptions, or even make your computer stop working, requiring a repair.

4
  • Thanks for your response. But, if the CPU has only one core. I cannot figure how it will manage both threads of the two separate process. The application is exactly equals, is something like a webscrapper. And both are running isoletaded from the other. – gog Jul 15 '15 at 14:22
  • 5
    This is called preemptive multi-tasking. The OS will set a time-slice (usually between 1 - 10 milliseconds), during which exactly one thread will be executing exclusively. When that time-slice is up, the OS will stop it, save the "execution context" of the thread in memory, and then give the next time-slice to another thread. When a thread performs a Thread.Sleep operation, it tells the OS that (1) it will give up its remaining time-slice, and (2) it will not need another time-slice until that sleep time has passed. Therefore, the OS will give time-slices to other threads. – rwong Jul 15 '15 at 14:25
  • Can you please include that in the answer? Preemptive multitasking is key for a complete and correct answer, and users shouldn't have to dig through the comments to find the rest of the answer. – acelent Jul 15 '15 at 16:40
  • @PauloMadeira : Originally I thought this question would be deleted soon, so I try to keep my initial answer short. (Also, I'm not sure if my description of the topic is accurate or not. The description is too much simplified from the perspective of today's OS versions.) – rwong Jul 15 '15 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.