Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Suppose there are hundread of threads waiting for entering in lock/Mutex section.

How can I release them all without executing process
by single line code (means by mainting bool variable it is possible)

thanks in advance
Posted

1 solution

Hundreds of threads waiting on the same resource sound a bit . . . dodgy, but regardless:

using a lock and bool like this:
volatile bool exit = false;

void WorkerFunc()
{
    lock(something)
    {
        if (exit) 
            return;

        // continue . . .
    }
}

will cause the threads to wake up serially and die one at a time as each one acquires/releases the lock.

With the use of a Mutex and ManualResetEvent then each thread can use WaitForMultipleObjects and wait for both objects. If the mutex is signaled then the thread can access the resource, else if the event is signaled the thread knows to exit instead. The wakeup and die can be done in parallel (although that's one impressive machine that can physically wake up hundreds of threads all at once). Then the single line of code to kill all threads would be a simple setting of the shutdown event.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900