1
DBThread::DBThread() : running_(false)
{
}
DBThread::~DBThread()
{
if (thread_)
{
thread_->join();
}
}
void DBThread::Init()
{
thread_ = std::make_shared<std::thread>(std::bind(&DBThread::Run, this));
std::unique_lock<std::mutex> lock(mutex_);
cv_.wait(lock, [&] {return running_; });
std::cout << "Init success";
}
void DBThread::AddTask(std::shared_ptr<Delegate> task)
{
std::lock_guard<std::mutex> lock(mutex_);
task_queue_.push(task);
}
void DBThread::Run()
{
running_ = true;
cv_.notify_all();
while (true)
{
std::unique_lock<std::mutex> lock(mutex_);
cv_.wait(lock, [&] {return !task_queue_.empty(); });
std::cout << "run task" << std::endl;
}
}
我有两个线程,我们将其命名为A和B,调用Init并等待B完全初始化,即使running_是true,也有时会挂起等待。这发生。任何帮助将不胜感激。notify_all没有唤醒等待线程
'running_'是什么类型? 'bool'或'std :: atomic'? –
ildjarn
running_纯粹是bool,我曾尝试将它声明为std :: atomic,仍然不起作用 –
user2260241
@ user2260241在C++中,默认情况下没有什么是保证原子的,甚至不是'bool'。您必须同步对'running_'的访问,否则您将进行数据竞赛。 – Snps