我已经使用gcc 8.2.1在Linux下编译了以下虚拟程序:
#include <iostream>
#include <mutex>
#include <thread>
struct Foo
{
void start() {
thread = std::thread(&Foo::run, this);
}
void stop() {
mutex.lock();
done = true;
mutex.unlock();
thread.join();
}
void run() {
bool tmp;
for (;;) {
mutex.lock();
tmp = done;
mutex.unlock();
if (tmp)
break;
}
}
std::thread thread;
std::mutex mutex;
bool done;
};
int main()
{
Foo foo;
std::cout << "starting...\n";
foo.start();
std::cout << "stopping...\n";
foo.stop();
std::cout << "done\n";
}
如果随后在valgrind 3.14.0下运行它,则会收到以下警告:
==30060== Thread 2:
==30060== Conditional jump or move depends on uninitialised value(s)
==30060== at 0x1095F3: Foo::run() (in /.../a.out)
==30060== by 0x109AAE: void std::__invoke_impl<void, void (Foo::*)(), Foo*>(std::__invoke_memfun_deref, void (Foo::*&&)(), Foo*&&) (in /.../a.out)
==30060== by 0x109771: std::__invoke_result<void (Foo::*)(), Foo*>::type std::__invoke<void (Foo::*)(), Foo*>(void (Foo::*&&)(), Foo*&&) (in /.../a.out)
==30060== by 0x10A012: decltype (__invoke((_S_declval<0ul>)(), (_S_declval<1ul>)())) std::thread::_Invoker<std::tuple<void (Foo::*)(), Foo*> >::_M_invoke<0ul, 1ul>(std::_Index_tuple<0ul, 1ul>) (in /.../a.out)
==30060== by 0x109FB8: std::thread::_Invoker<std::tuple<void (Foo::*)(), Foo*> >::operator()() (in /.../a.out)
==30060== by 0x109F8D: std::thread::_State_impl<std::thread::_Invoker<std::tuple<void (Foo::*)(), Foo*> > >::_M_run() (in /.../a.out)
==30060== by 0x496A062: execute_native_thread_routine (thread.cc:80)
==30060== by 0x4894A9C: start_thread (in /usr/lib/libpthread-2.28.so)
==30060== by 0x4CD7A42: clone (in /usr/lib/libc-2.28.so)
我不确定是什么原因造成的,我已经编写了此代码段,以期诊断当前正在处理的更复杂的类中的错误(我无法在此处发布),并且在调用等效的时会产生异常Foo::stop()
。 valgrind警告是否暗示我对C ++线程接口有严重的误解?并假设Foo::run
实际上会做一些有用的事情,如何在保持Foo
的界面不变的情况下修复该程序?
答案 0 :(得分:5)
bool done;
的初始值是多少?它是不确定的(有些垃圾值),因此可以在不调用run
方法的情况下停止线程(stop
。
done
必须初始化:
//...
std::mutex mutex;
bool done = false; // <--