如何释放等待Mutex

如何释放等待Mutex

本文介绍了如何释放等待Mutex,Lock的线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设有大量线程等待在锁/互斥部分中输入.

如何在不执行进程的情况下全部释放它们
通过单行代码(意味着可以维护bool变量)

在此先感谢

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

推荐答案

volatile bool exit = false;

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

        // continue . . .
    }
}


将导致线程串行唤醒,并在每个线程获取/释放锁时一次死亡.

通过使用Mutex和ManualResetEvent,每个线程都可以使用WaitForMultipleObjects并等待两个对象.如果发出了互斥信号,则线程可以访问资源,否则,如果发出了信号,则线程知道退出.唤醒和死亡可以并行完成(尽管这是一台令人印象深刻的机器,可以一次物理唤醒数百个线程).那么杀死所有线程的单行代码将是关闭事件的简单设置.


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.


这篇关于如何释放等待Mutex,Lock的线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 05:47