有效地等待标志状态更改而不阻塞资源?

时间:2022-04-07 00:16:58

Thread to wait infinitely in a loop until a flag state change, then call function.

线程在循环中无限期等待,直到标志状态改变,然后调用函数。

pseudo code illustration:

伪代码图:

while (true)
{
    while (!flag)
    {
            sleep(1);
    }
    clean_upfunction();
}

Currently:

No:

  • MFC

Question:

  • Is there a more efficient way of implementing the above
  • 有没有更有效的方法来实现上述

  • A waitForStateChange() - similar to above - in the threading library
  • 一个waitForStateChange() - 类似于上面 - 在线程库中

3 个解决方案

#1


For Windows (which you have this tagged for), you want to look at WaitForSingleObject. Use a Windows Event (with CreateEvent), then wait on it; the other thread should call SetEvent. All native Windows, no MFC or anything else required.

对于Windows(您已将其标记为),您需要查看WaitForSingleObject。使用Windows事件(使用CreateEvent),然后等待它;另一个线程应该调用SetEvent。所有本机Windows,没有MFC或任何其他要求。

#2


If you're not on Windows, and are instead on a POSIXish box, pthread_cond_wait is the best match:

如果您不在Windows上,而是在POSIXish框中,则pthread_cond_wait是最佳匹配:

/* signaler */
    pthread_mutex_lock(mutex);
    flag = true;
    pthread_cond_signal(cond);
    pthread_mutex_unlock(mutex);

/* waiter */
    pthread_mutex_lock(mutex);
    do {
        pthread_cond_wait(cond, mutex);
    } while (!flag);
    pthread_mutex_unlock(mutex);

The classic self-pipe trick is easier and cooler though :) Works on systems without pthreads too.

经典的自管技巧虽然更容易和更酷:)适用于没有pthreads的系统。

/* setup */
    int pipefd[2];
    if (pipe(pipefd) < 0) {
        perror("pipe failed");
        exit(-1);
    }

/* signaler */
    char byte = 0;
    write(pipefd[0], &byte, 1);  // omitting error handling for brevity

/* waiter */
    char byte;
    read(pipefd[1], &byte, 1);  // omitting error handling for brevity

The waiter will block on the read (you don't set O_NONBLOCK) until interrupted (which is why you should have error handling) or the signaler writes a byte.

服务员将阻止读取(您不设置O_NONBLOCK)直到被中断(这就是您应该进行错误处理的原因)或者信号器写入一个字节。

#3


Take a look at condition_variable in Boost.Thread.

看看Boost.Thread中的condition_variable。

http://www.boost.org/doc/libs/1_37_0/doc/html/thread/synchronization.html#thread.synchronization.condvar_ref

It is portable, easier to use than the platform-specific options. Moreover, IIUC, the upcoming C++0x std::condition_variable was modeled after it.

它比便携式设备更容易使用,比特定于平台的选项更容易使用。此外,IIUC,即将推出的C ++ 0x std :: condition_variable是在它之后建模的。

#1


For Windows (which you have this tagged for), you want to look at WaitForSingleObject. Use a Windows Event (with CreateEvent), then wait on it; the other thread should call SetEvent. All native Windows, no MFC or anything else required.

对于Windows(您已将其标记为),您需要查看WaitForSingleObject。使用Windows事件(使用CreateEvent),然后等待它;另一个线程应该调用SetEvent。所有本机Windows,没有MFC或任何其他要求。

#2


If you're not on Windows, and are instead on a POSIXish box, pthread_cond_wait is the best match:

如果您不在Windows上,而是在POSIXish框中,则pthread_cond_wait是最佳匹配:

/* signaler */
    pthread_mutex_lock(mutex);
    flag = true;
    pthread_cond_signal(cond);
    pthread_mutex_unlock(mutex);

/* waiter */
    pthread_mutex_lock(mutex);
    do {
        pthread_cond_wait(cond, mutex);
    } while (!flag);
    pthread_mutex_unlock(mutex);

The classic self-pipe trick is easier and cooler though :) Works on systems without pthreads too.

经典的自管技巧虽然更容易和更酷:)适用于没有pthreads的系统。

/* setup */
    int pipefd[2];
    if (pipe(pipefd) < 0) {
        perror("pipe failed");
        exit(-1);
    }

/* signaler */
    char byte = 0;
    write(pipefd[0], &byte, 1);  // omitting error handling for brevity

/* waiter */
    char byte;
    read(pipefd[1], &byte, 1);  // omitting error handling for brevity

The waiter will block on the read (you don't set O_NONBLOCK) until interrupted (which is why you should have error handling) or the signaler writes a byte.

服务员将阻止读取(您不设置O_NONBLOCK)直到被中断(这就是您应该进行错误处理的原因)或者信号器写入一个字节。

#3


Take a look at condition_variable in Boost.Thread.

看看Boost.Thread中的condition_variable。

http://www.boost.org/doc/libs/1_37_0/doc/html/thread/synchronization.html#thread.synchronization.condvar_ref

It is portable, easier to use than the platform-specific options. Moreover, IIUC, the upcoming C++0x std::condition_variable was modeled after it.

它比便携式设备更容易使用,比特定于平台的选项更容易使用。此外,IIUC,即将推出的C ++ 0x std :: condition_variable是在它之后建模的。