use*_*288 0 c++ linux multithreading boost mutex
我正在Linux上进行多线程C++提升.
即使我尝试使用锁,以下程序仍然具有竞争条件.
结果是8或9或5.它不应该发生.
#include <iostream>
#include <boost/bind.hpp>
#include <boost/threadpool.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread.hpp>
boost::mutex myMutex ;
int g = 0 ;
void f()
{
//myMutex.lock();
{
boost::mutex::scoped_lock lock(myMutex);
++g;
}
//myMutex.unlock();
return ;
}
const int threadnum = 10;
int main()
{
boost::threadpool::fifo_pool tp(threadnum);
for (int i = 0 ; i < threadnum ; ++i)
tp.schedule(boost::bind(f));
std::cout << g << std::endl ;
return 0 ;
}
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.
谢谢 !
来自http://threadpool.sourceforge.net/tutorial/intro.html:
了解该任务仅计划执行非常重要.计划立即返回,并且不保证任务何时执行以及处理将花费多长时间.
您可以安排10个任务,然后立即将结果打印到您到达生产线时执行的数量
std :: cout << g << std :: endl;
因此,虽然您的互斥锁确保线程一次增加一个g,但您不会在打印结果之前等待它们完成.修改代码的一种方法是等待池中的所有任务完成:
boost::threadpool::fifo_pool tp(threadnum);
for (int i = 0 ; i < threadnum ; ++i)
tp.schedule(boost::bind(f));
tp.wait(); //WAIT FOR TASKS TO EXECUTE
std::cout << g << std::endl ;
return 0 ;
Run Code Online (Sandbox Code Playgroud)
我不确定我是否正确阅读,但看起来你正在安排一些会增加g的东西,然后在g的内容上调用cout.你的互斥体会阻止预定的触发器相互踩踏,但是没有任何东西迫使cout在最后等待它们全部完成.你需要某种读/写互斥锁.