在信号处理程序中使用互斥锁
Using mutexes in signal handers
我有一个多线程应用程序,需要在出现中断、终止等信号时优雅地停止它。
这里有一个片段来说明逻辑的相关部分。
std::atomic_bool running = true;
std::mutex mutex;
std::condition_variable condition;
void signal_handler(int signal)
{
// in another thread `[&]() { return !running || something_to_do_conditon; } == false`
// what means continue to wait
running = false;
condition.notify_one();
// another thread goes to sleep
}
void run() {
while(true) {
std::unique_lock lock(mutex);
condition.wait_for(lock, std::chrono::hours(1), [&]() { return !running || something_to_do_conditon; });
if (!running) {
return;
}
// do smth
}
}
int main()
{
// Install a signal handler
std::signal(SIGINT, signal_handler);
std::signal(SIGTERM, signal_handler);
std::thread thread(run);
thread.join();
}
正如您在 signal_handler
中看到的那样,即使 running
设置为 false
,condition
也会收到通知,但仍有一种情况(描述带有内联注释)线程进入休眠状态 1 小时。发生这种情况是因为 running
变量周围没有互斥体。这允许线程在设置变量之前锁定互斥锁并检查条件。如果我添加类似
{
std::lock_guard<std::mutex> lock(mutex);
running = false;
}
在将要避免的处理程序中。
那么问题是如何使用(是否可能)互斥量而不会出现潜在的死锁或任何其他问题。从信号中削弱休眠线程的任何其他技巧。
在 pthreads 程序中处理信号的可靠方法是屏蔽您希望在每个线程中处理的所有信号,并创建一个循环调用 sigwaitinfo()
(或 sigtimedwait()
).
信号处理线程然后可以使用普通的受互斥锁保护的共享变量和 pthread_cond_signal()
/ pthread_cond_broadcast()
唤醒通知其他线程接收到的信号。
在您的示例中,专用信号处理线程可以在更改 running
标志之前安全地锁定互斥锁,因为它只是在普通线程上下文中,而不是信号处理程序。
我有一个多线程应用程序,需要在出现中断、终止等信号时优雅地停止它。
这里有一个片段来说明逻辑的相关部分。
std::atomic_bool running = true;
std::mutex mutex;
std::condition_variable condition;
void signal_handler(int signal)
{
// in another thread `[&]() { return !running || something_to_do_conditon; } == false`
// what means continue to wait
running = false;
condition.notify_one();
// another thread goes to sleep
}
void run() {
while(true) {
std::unique_lock lock(mutex);
condition.wait_for(lock, std::chrono::hours(1), [&]() { return !running || something_to_do_conditon; });
if (!running) {
return;
}
// do smth
}
}
int main()
{
// Install a signal handler
std::signal(SIGINT, signal_handler);
std::signal(SIGTERM, signal_handler);
std::thread thread(run);
thread.join();
}
正如您在 signal_handler
中看到的那样,即使 running
设置为 false
,condition
也会收到通知,但仍有一种情况(描述带有内联注释)线程进入休眠状态 1 小时。发生这种情况是因为 running
变量周围没有互斥体。这允许线程在设置变量之前锁定互斥锁并检查条件。如果我添加类似
{
std::lock_guard<std::mutex> lock(mutex);
running = false;
}
在将要避免的处理程序中。
那么问题是如何使用(是否可能)互斥量而不会出现潜在的死锁或任何其他问题。从信号中削弱休眠线程的任何其他技巧。
在 pthreads 程序中处理信号的可靠方法是屏蔽您希望在每个线程中处理的所有信号,并创建一个循环调用 sigwaitinfo()
(或 sigtimedwait()
).
信号处理线程然后可以使用普通的受互斥锁保护的共享变量和 pthread_cond_signal()
/ pthread_cond_broadcast()
唤醒通知其他线程接收到的信号。
在您的示例中,专用信号处理线程可以在更改 running
标志之前安全地锁定互斥锁,因为它只是在普通线程上下文中,而不是信号处理程序。