简体   繁体   English

其他线程正在等待时如何阻塞线程

[英]How to block a thread while other threads are waiting

I have a very specific problem to solve. 我有一个非常具体的问题要解决。 I'm pretty sure someone else in the world has already encountered and solved it but I didn't find any solutions yet. 我很确定世界上其他人已经遇到并解决了它,但是我还没有找到任何解决方案。

Here it is : 这里是 :

  • I have a thread that pop command from a queue and execute them asynchronously 我有一个线程从队列中弹出命令并异步执行它们
  • I can call from any other thread a function to execute a command synchronously, bypassing the queue mechanism, returning a result, and taking priority of execution (after the current execution is over). 我可以从任何其他线程调用函数来同步执行命令,绕过队列机制,返回结果,并优先执行(在当前执行结束之后)。
  • I have a mutex protecting a command execution so only one is executed at a time The problem is, with a simple mutex, I have no certitude that a synchronous call will get the mutex before the asynchronous thread when in conflict. 我有一个互斥锁来保护命令的执行,所以一次只能执行一个。问题是,对于一个简单的互斥锁,我不确定在冲突时同步调用会在异步线程之前获得互斥锁。 In fact, our test shows that the allocation is very unfair and that the asynchronous thread always win. 实际上,我们的测试表明分配是非常不公平的,并且异步线程始终会获胜。

So I want to block the asynchronous thread while there is a synchronous call waiting. 因此,我想在有一个同步调用等待时阻塞异步线程。 I don't know in advance how many synchronous call can be made, and I don't control the threads that make the calls (so any solution using a pool of threads is not possible). 我事先不知道可以进行多少次同步调用,并且我不控制进行调用的线程(因此无法使用线程池进行任何解决方案)。

I'm using C++ and Microsoft library. 我正在使用C ++和Microsoft库。 I know the basic synchronization objects, but maybe there is an more advance object or method suitable for my problem that I don't know. 我知道基本的同步对象,但是也许有一个更高级的对象或方法适合我所不知道的问题。

I'm open to any idea! 我愿意接受任何想法!

Ok so I finally get the chance to close this. 好的,所以我终于有机会关闭它。 I tried some of the solution proposed here and in the link posted. 我尝试了此处和链接中提出的一些解决方案。 In the end, I combined a mutex for the command execution and a counter of awaiting sync calls (the counter is also protected by a mutex of course). 最后,我将用于执行命令的互斥锁与等待同步调用的计数器结合在一起(该计数器当然也受互斥锁保护)。 The async thread check the counter before trying to get the mutex, and wait the counter to be 0. Also, to avoid a loop with sleep, I added an event that is set when the counter is set to 0. The async thread wait for this event before trying to get the mutex. 异步线程在尝试获取互斥之前先检查计数器,然后等待计数器为0。此外,为避免出现睡眠循环,我添加了一个在计数器设置为0时设置的事件。异步线程等待尝试获取互斥对象之前发生此事件。

void incrementSyncCounter()
{
    DLGuardThread guard(_counterMutex);
    _synchCount++;
}

void decrementSyncCounter()
{
    DLGuardThread guard(_counterMutex);
    _synchCount--;

    // If the counter is 0, it means that no other sync call is waiting, so we notify the main thread by setting the event
    if(_synchCount == 0)
    {
        _counterEvent.set();
    }
}

unsigned long getSyncCounter()
{
    DLGuardThread guard(_counterMutex);
    return _synchCount;
}

bool executeCommand(Command* command)
{
    // Increment the sync call counter so the main thread can be locked while at least one sync call is waiting
    incrementSyncCounter();

    // Execute the command using mutex protection
    DLGuardThread guard(_theCommandMutex);
    bool res = command->execute();
    guard.release();

    // Decrement the sync call counter so the main thread can be unlocked if there is no sync call waiting
    decrementSyncCounter();

    return res;
}

void main ()
{
    [...]
    // Infinite loop
    while(!_bStop)
    {
        // While the Synchronous call counter is not 0, this main thread is locked to give priority to the sync calls.
        // _counterEvent will be set when the counter is decremented to 0, then this thread will check the value once again to be sure no other call has arrived inbetween.
        while(getSyncCounter() > 0)
        {
            ::WaitForSingleObject (_counterEvent.hEvent(), INFINITE);
        }

        // Take mutex
        DLGuardThread guard(_theCommandMutex);

        status = command->execute();

        // Release mutex
        guard.release();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C++ 线程:如何在另一个线程仍在运行时停止一个线程的执行 (Win32) - C++ threads: How to stop one thread from executing while other is still running (Win32) C++ 如何确定互斥锁是否被单个线程不成比例地占用,同时阻塞其他线程 - C++ how to figure if a mutex is being disproportionately hogged by a single thread while blocking other threads 如何在CUDA的块中返回其他线程? - How do I return other threads in a block in CUDA? 如何使用boost :: thread临时暂停其他线程? - how can I temporarily pause other threads using boost::thread? pthreads:触发其他线程的线程 - pthreads: a thread that triggers other threads 退出进程时等待线程完成 - Waiting on threads to finish while quitting the process QWaitCondition:在线程仍在等待时被销毁 - QWaitCondition: Destroyed while threads are still waiting 阻塞主线程以等待其子线程 - block the main thread to wait for its child threads C++ 应用程序的多个线程在(取消)分配内存时相互阻塞 - Multiple Threads of C++ application block each other while (de-)allocating memory 当其他线程正在编写线程安全时,我是否必须互斥读取操作? - Do i have to mutex a reading operation while other threads are writing thread safe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM