gpt4 book ai didi

c++ - 增加计数器时避免竞争条件

转载 作者:行者123 更新时间:2023-11-30 04:26:46 25 4
gpt4 key购买 nike

我以前发过关于这个主题的帖子,但到目前为止我运气不太好。我把它归结为我的一个坏问题。这次我做了一个简短的可编译示例,显示了我试图避免的不良行为。我希望这是值得赞赏的。

问题是两个(或更多)线程被设置为运行同一个进程,它们的“id”决定了它们对可变数据的哪一部分进行操作。目前两个线程都会更新计数器。

当前输出看起来像这样,

tid = 0, var[tid] = 0
tid = 0, var[tid] = 1
tid = 0, var[tid] = 2
tid = 0, var[tid] = 3
tid = 0, var[tid] = 4
tid = 0, var[tid] = 5
tid = 0, var[tid] = 6
tid = 0, var[tid] = 7
tid = 0, var[tid] = 8
tid = 0, var[tid] = 9
tid = 1, var[tid] = 0
Press any key to continue . . .

期望的输出应该是这样的......

tid = 0, var[tid] = 0
tid = 1, var[tid] = 0
tid = 0, var[tid] = 1
tid = 1, var[tid] = 1
tid = 0, var[tid] = 2
tid = 1, var[tid] = 2
tid = 0, var[tid] = 3
tid = 1, var[tid] = 3 etc.

如有任何指导,我们将不胜感激。

编辑:我用按预期工作的代码更新了答案。

[注意这里效率很重要,我想尽快完成流程]

#include <iostream>  
#include <boost/thread.hpp>

int var[2];
int mT;
int mTotalSamples;
boost::mutex mCountMutex;
boost::thread *threadMap[2];

using namespace std;

void process()
{
int tid = 1;

// sleep for 1 seconds - just to make sure threadMap
// has been assigned (only ncessary for this demo).
boost::this_thread::sleep(boost::posix_time::seconds(1));

if (threadMap[0]->get_id() == boost::this_thread::get_id()){ tid = 0;}

while ( mT < mTotalSamples )
{
// perform processing
var[tid] = mT;
// processing complete

mCountMutex.lock(); // (a thread waits to aquire mutex)
cout << "tid = " << tid << ", var[tid] = " << var[tid] << endl;
mT++; // How to stop both threads incrementing this?
mCountMutex.unlock();
}
}

int main()
{
boost::thread_group threads;

mT = 0;
mTotalSamples = 10;

threadMap[0] = threads.create_thread( boost::bind(&process) );
threadMap[1] = threads.create_thread( boost::bind(&process) );

threads.join_all();

return 0;
}

最佳答案

从您的预期输出来看,您希望您的线程在每次更新后同步。 boost 库提供了 boost::barrier如果您在 process 中的 while 循环的开始或结束处放置一个 wait,应该可以解决问题。

#include <iostream>  
#include <boost/thread.hpp>

int var[2];
int mT;
int mTotalSamples;
boost::mutex mCountMutex;
boost::thread *threadMap[2];
boost::barrier bar(2);

using namespace std;

void process()
{
int tid = 1;

// sleep for 2 seconds - just to make sure threadMap
// has been assigned (only ncessary for this demo).
boost::this_thread::sleep(boost::posix_time::seconds(2));

if (threadMap[0]->get_id() == boost::this_thread::get_id()){ tid = 0;}

while ( mT < mTotalSamples )
{
// perform processing
var[tid] = mT;
// processing complete


bar.wait();
if (threadMap[0]->get_id() == boost::this_thread::get_id())
{
mT++;
cout << "var[0] = " << var[0] << endl;
cout << "var[1] = " << var[1] << endl;
}
bar.wait();
}
}

int main()
{
boost::thread_group threads;

mT = 0;
mTotalSamples = 10;

threadMap[0] = threads.create_thread( boost::bind(&process) );
threadMap[1] = threads.create_thread( boost::bind(&process) );

threads.join_all();

return 0;
}

关于c++ - 增加计数器时避免竞争条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11335513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com