例如:
struct A {
void run1() {}
void run2() {}
};
int main()
{
A a;
thread t1(bind(&A::run1, ref(a)));
thread t2(bind(&A::run2, ref(a)));
// not joinable thread!
int i;
std::cin >> i;
}
请看这个例子http://developer.gnome.org/glibmm/unstable/thread_2thread_8cc-example.html
a 对象是否应该由互斥锁或其他东西保护?这里没有竞争条件吗?
最佳答案
这取决于方法中的代码。如果方法中没有比赛,那很好。您的示例中没有,因为没有代码;)
在您的示例中,“a”可能会在线程退出之前被销毁(如果用户速度很快)。
关于c++ - 在不同线程中作为一个类的成员函数使用安全吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9241222/