The*_*ror 1 c++ multithreading boost vector
我有我的主要类:Foo有一个名为的变量
std::vector<Something*> X;
Run Code Online (Sandbox Code Playgroud)
和功能
SideThreadUpdate();
MainThreadUpdate();
Run Code Online (Sandbox Code Playgroud)
如果我for在我的循环中SideThreadUpdate:
for(int i = 0; i < X.size() ; i++)
{
X->randomBool = true;
}
Run Code Online (Sandbox Code Playgroud)
在我的这个MainThreadUpdate:
X.push_back(new Something());
Run Code Online (Sandbox Code Playgroud)
这将给我访问违规和其他奇怪的错误.我怎样才能使这个线程安全?
我需要访问X两个线程中的变量,我boost::thread用于多线程..
我似乎无法boost::mutex开启 std::vector<Something*> X;
此外,我需要连续访问变量,不能让MainThread或SideThread等待..
我的用例的简化示例; 我有1个线程,我想检查冲突,我的其他线程来处理输入和其他东西,都需要访问Xl