我想做一个控制回路接口。
它有should_stop()方法来检查循环是否应该中断或继续。
它确实侦听SIGINT信号(Ctrl+C),并在得到信号后,should_stop()方法返回True。
现在看来,这个接口正在工作。
但我不确定这个界面是否线程安全。
LoopInterface.h文件
#include "signal.h"
#include "pthread.h"
#define LOCK(mutex) pthread_mutex_lock(&mutex)
#define UNLOCK(mutex) pthread_mutex_unlock(&mutex)
class LoopInterface {
public:
LoopInterface(){
LOCK(_lock_is_signal_registered);
bool temp = _is_signal_registered;
UNLOCK(_lock_is_signal_registered);
if(!temp)
{
register_signal();
}
}
bool should_stop()
{
LOCK(_lock_should_stop);
bool temp = _should_stop;
UNLOCK(_lock_should_stop);
return _should_stop;
}
private:
static void register_signal()
{
LOCK(_lock_is_signal_registered);
_is_signal_registered = true;
UNLOCK(_lock_is_signal_registered);
signal(SIGINT, &LoopInterface::signal_handler);
}
static void signal_handler(int sig){
LOCK(_lock_should_stop);
_should_stop = true;
UNLOCK(_lock_should_stop);
}
static bool _should_stop;
static bool _is_signal_registered;
static pthread_mutex_t _lock_should_stop, _lock_is_signal_registered;
};LoopInterface.cpp文件
#include "LoopInterface.h"
bool LoopInterface::_should_stop = false;
bool LoopInterface::_is_signal_registered = false;
pthread_mutex_t LoopInterface::_lock_should_stop;
pthread_mutex_t LoopInterface::_lock_is_signal_registered;这就是它的使用方式。
/************Threads*************/
#include "LoopInterface.h"
class A : public LoopInterface{
};
void threadX(){
A a;
while(!a.should_stop()){
//do something...
}
}你能告诉我这个接口能安全地工作吗?还是不想?
如果没有,问题是什么?
附加问题
我的同步代码还有一个问题。
死锁经常发生,因为在pthread_mutex_lock中调用signal_handler,而should_stop方法则锁定相同的mutex。
我找到了一个article,它声明在信号处理程序中不应该调用与线程相关的函数。
我认为我应该找到另一种方法来同步我的成员变量。
发布于 2015-08-23 11:35:51
显然,这段代码并不是线程安全的:变量_should_stop是由某个不受控制的线程中的信号设置的,并在运行自己的线程的LoopInterface中读取。根本没有同步。这种方法只能在只有一个线程的情况下才是线程安全的.
可以使用以下方法使代码线程安全(与此特定变量有关)
static std::atomic<bool> _should_stop;由于此时您对任何其他值都不感兴趣,所以可以使用std::memory_order_relaxed标志读取变量。
当然,如果构造函数的LoopInterface是从多个线程调用的,则还需要同步对_is_signal_registered的访问。
https://stackoverflow.com/questions/32166135
复制相似问题