Synchronization: Wake up a thread when all other threads have left - multithreading

Here's the scenario:
When my application is shutting down I wish to start a disposing thread but it must sleep straight away. I want all the running threads enter a cleanup section. Once all of those threads have finished their job and left the section, I want the disposing section wake up and finaliaze the shut down process.
Which synchronization mechanism should I use, Mutex, Monitor, or Semaphore? and How?
Thanks.

Related

Daemon And Non-Daemon Threads In Java

JVM in Java is responsible to create a Non-Daemon Thread when executing a Java program. Is it correct?
If so, who is responsible to create Daemon Threads in Java?
Programmers and JVM both create Non-Daemon Threads? Is it correct?
Looking for a clear explanation.
Thanks in Advance.
It doesn’t matter whether “the JVM” or “the programmer” started a thread. A thread is a daemon thread when setDaemon(true) has been called on it before starting or a daemon thread created the thread without calling setDaemon. That’s it.
The documentation of Thread also says:
When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main of some designated class).
There is no responsibility to create daemon threads. Marking a thread as daemon has only one implication which the documentation continues to explain:
The Java Virtual Machine continues to execute threads until either of the following occurs:
The exit method of class Runtime has been called and the security manager has permitted the exit operation to take place.
All threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an exception that propagates beyond the run method.
So, that’s the only implication; the existence of a non-daemon thread may prevent the JVM from terminating automatically, whereas threads marked as daemon do not.

What does it mean by 'The thread that releases a semaphore need not be the same thread that acquired it'?

In Semaphore vs. Monitors - what's the difference?,
The thread that releases a semaphore need not be the same thread that
acquired it.
if a thread didn't acquire a semaphore, then how can the thread release the semaphore?
Does the semaphore here mean a semaphore implemented with busy waiting, or implemented with process blocking?
Thanks.
A semaphore covers different use cases than a mutex.
With a mutex, you would be right. A mutex is typically used to prevent concurrent execution of critical sections in the code. A particular thread will acquire the mutex at the start of the critical section and release it when it leaves the critical section again. Having a mutex being released by a different thread than the one that acquired it would mean that the critical section is spanned across multiple threads, which is most probably not how you want to do things.
The use case for a semaphore is slightly different. It signals availability of a resource. A thread that needs to consume a resource will acquire the semaphore, which is conceptually acquiring the underlying resource. If no resources are available, the acquire will block.
Now, in the scenario where we talk about a fixed set of resources (like a set of available I/O ports) it makes sense for the acquiring thread to also release the resource again. I acquire the port, do some work and release it when I'm done so that other threads can do work on it.
But this is not the only use case for semaphores. Think of producer/consumer: A producer thread might provide resources (like items that are queued for processing by a worker thread) and the consumer thread will accept them. In these scenarios, consumed resources are typically gone, so you do not release your resources after acquiring them. Instead, the producing thread calls release to indicate that there is stuff available for consumption. The consumer then calls acquire to claim a produced resource and process it. A producer will never call acquire and a consumer will never call release.
In many situations, the thread that acquired a semaphore can't release it, because it is blocked waiting for someone to release the semaphore, and it must be some other thread releasing (signalling) the semaphore.
Implementing semaphores with busy waiting would be just absolutely awful. Unlike with locks, there are situations where semaphores are held for a long time (seconds or minutes, hours would be a bit unusual but is absolutely possible).
Obviously a reference to the semaphore object needs to be stored in a place where another thread can access it.

Does a thread that is blocked cause the process to become blocked? Why and How?

Does a thread that is blocked cause the process to become blocked? Why and How? Thanks to all experts for answering.
A process cannot be blocked because the concept of "blocked" only applies to a thread of execution. The only meaningful sense in which you could say that a process was blocked is if the process only had one thread and that thread was blocked.
A thread is a flow of execution through the process code, with its own program counter, system registers and stack. A thread is also called a light weight process. Threads provide a way to improve application performance through parallelism. Threads represent a software approach to improving performance of operating system by reducing the overhead thread is equivalent to a classical process.
Each thread belongs to exactly one process and no thread can exist outside a process. Each thread represents a separate flow of control.Threads have been successfully used in implementing network servers and web server. They also provide a suitable foundation for parallel execution of applications on shared memory multiprocessors.
So, as you may have guessed, No ! A thread cannot block a process.

Windows service onstop will kill the thread?

I have a windows service where I create a thread for doing the background process. So once the thread finishes the task, do I need to call the service ‘stop’ event to make the service stop? (like this.stop() in code)
I didn’t clearly understand why it’s asking to not write code in onstart event and asked to create a thread. Is it because the service will stop after a particular time? Will it cause my thread also to stop, when the service ends? Because I don’t need that. The thread should handle a long running process; I need the thread not killed until it finishes the task.

Scheduling the created thread immediately

i am using PsCreateSystemThread() for creating a thread. But it is not getting scheduled immediately. How can i force that thread schedule immediately after the creation of the thread.
Scheduling can be forced by KeSetPriorityThread and/or KeSetBasePriorityThread.

Resources