Multithreading 如何在两个C++;11条线?
情况: 一个Multithreading 如何在两个C++;11条线?,multithreading,c++11,thread-synchronization,Multithreading,C++11,Thread Synchronization,情况: 一个Factory类成员函数manager生成两个worker线程。每个工作者线程在一年中的每一天运行一个循环,并增加他们自己的var工时,以及一个共享的var工时。每天结束时,每个员工线程向经理发送信号,以获取他们的工作报告和工作时间报告 当manager从worker两个线程获取workHours时,它将totalWorkHours作为(workHours[0]+workHours[1])报告给boss 问题: 线程未按预期工作。在向管理器报告之前,线程在多个循环中运行。它们不会在每
Factory
类成员函数manager
生成两个worker
线程。每个工作者
线程在一年中的每一天运行一个循环,并增加他们自己的var工时
,以及一个共享的var工时
。每天结束时,每个员工
线程向经理
发送信号,以获取他们的工作报告
和工作时间报告
当manager
从worker
两个线程获取workHours
时,它将totalWorkHours
作为(workHours[0]+workHours[1]
)报告给boss
问题:
线程未按预期工作。在向管理器报告之前,线程在多个循环中运行。它们不会在每天的报告中同步。
如何实现线程同步,即每天之后,两个线程都向管理器报告其状态
代码:
[下面的代码没有编译错误,如果您想测试运行,请编辑]
#include <iostream>
#include <string>
#include <thread>
#include <mutex>
using namespace std;
class Factory{
public:
Factory() {
work = 0;
workHours[0] = 0;
workHours[1] = 0;
};
void manager();
private:
std::thread threads[2];
std::mutex m_lock;
std::condition_variable cond[2];
int work; // Shared across threads
int workHours[2]; // One var for each thread
int totalWorkHours;
void worker(int id);
void boss(int work, int workHours);
};
void Factory::worker(int id)
{
cout<< id <<" Started Working "<<endl;
for (int day = 0; day<365; day++)
{
std::unique_lock<std::mutex> lck{ m_lock };
work++;
workHours[id]++;
cout << id << " working " << endl;
cond[id].notify_one();
lck.unlock();
std::this_thread::sleep_for(1s);
}
}
void Factory::manager()
{
int wHrs0, wHrs1;
threads[0] = std::thread([&](Factory *fac) { fac->worker(0); }, this);
threads[1] = std::thread([&](Factory *fac) { fac->worker(1); }, this);
//for (int day=0; day<365; day++)
for (;;)
{
std::unique_lock<mutex> lck0{ m_lock };
cond[0].wait(lck0);
cout << "Worker0 workHours : " << workHours[0] << " Total Work : " << work << endl;
wHrs0 = workHours[0];
lck0.unlock();
std::unique_lock<mutex> lck1{ m_lock };
cond[1].wait(lck1);
cout << "Worker1 workHours : " << workHours[1] << " Total Work : " << work << endl;
wHrs1 = workHours[1];
lck1.unlock();
totalWorkHours = wHrs0 + wHrs1;
cout << "Both Workers Worked one day" << endl;
boss(work, totalWorkHours);
}
}
void Factory::boss(int work, int workHours)
{
cout << "I am not Happy with just " << work << " amount of work in damn " << workHours << " Hrs " << endl;
}
int main()
{
Factory nike;
nike.manager();
//wait for keypress
cin.get();
return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
阶级工厂{
公众:
工厂(){
功=0;
工时[0]=0;
工作时间[1]=0;
};
无效管理器();
私人:
标准:螺纹[2];
std::互斥m_锁;
std::条件_变量cond[2];
int work;//跨线程共享
int workHours[2];//每个线程一个变量
总工作时间;
无效工作者(内部id);
无效老板(整数工时,整数工时);
};
无效工厂::工人(内部id)
{
我自己解决了。贴在这里供参考
worker
等待manager
使用工作结果。而manager
则等待两个worker
完成一天的工作
// Example program
#include <iostream>
#include <string>
#include <thread>
#include <mutex>
#include <condition_variable>
using namespace std;
class Factory{
public:
Factory() {
work = 0;
workHours[0] = 0;
workHours[1] = 0;
};
void manager();
private:
std::thread threads[2];
std::mutex m_lock;
std::condition_variable cond[2];
std::condition_variable condMan;
bool resFetch[2];
bool resAvailable[2];
int work; // Shared across threads
int workHours[2]; // One var for each thread
int totalWorkHours;
void worker(int id);
void boss(int work, int workHours);
};
void Factory::worker(int id)
{
cout<< id <<" Started Working "<<endl;
for (int day = 0; day<365; day++)
{
std::unique_lock<std::mutex> lck{ m_lock };
while(!resFetch[id])
condMan.wait(lck);
resFetch[id] = false;
work++;
workHours[id]++;
cout << id << " working " << endl;
resAvailable[id] = true;
cond[id].notify_one();
lck.unlock();
std::this_thread::sleep_for(1s);
}
}
void Factory::manager()
{
int wHrs0, wHrs1;
threads[0] = std::thread([&](Factory *fac) { fac->worker(0); }, this);
threads[1] = std::thread([&](Factory *fac) { fac->worker(1); }, this);
for (;;)
{
std::unique_lock<std::mutex> lck{ m_lock };
resFetch[0] = true;
resFetch[1] = true;
condMan.notify_all();
while(!resAvailable[0])
cond[0].wait(lck);
cout << "Worker0 workHours : " << workHours[0] << " Total Work : " << work << endl;
wHrs0 = workHours[0];
resAvailable[0] = false;
while (!resAvailable[1])
cond[1].wait(lck);
cout << "Worker1 workHours : " << workHours[1] << " Total Work : " << work << endl;
wHrs1 = workHours[1];
resAvailable[1] = false;
lck.unlock();
totalWorkHours = wHrs0 + wHrs1;
cout << "Both Workers Worked one day" << endl;
boss(work, totalWorkHours);
}
}
void Factory::boss(int work, int workHours)
{
cout << "I am not Happy with just " << work << " amount of work in damn " << workHours << " Hrs " << endl;
}
int main()
{
Factory nike;
nike.manager();
//wait for keypress
cin.get();
return 0;
}
//示例程序
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
阶级工厂{
公众:
工厂(){
功=0;
工时[0]=0;
工作时间[1]=0;
};
无效管理器();
私人:
标准:螺纹[2];
std::互斥m_锁;
std::条件_变量cond[2];
std::条件变量condMan;
bool-resFetch[2];
布尔可再利用[2];
int work;//跨线程共享
int workHours[2];//每个线程一个变量
总工作时间;
无效工作者(内部id);
无效老板(整数工时,整数工时);
};
无效工厂::工人(内部id)
{
你可能想在你的工作者
循环睡眠前解锁锁。@Someprogrammerdude,谢谢你的评论。我这样做了(也更新了问题),但仍然没有正确同步。在限制下,工作者(1)
可以在管理器
到达cond[1]之前运行所有365次循环迭代。等等(lck1)
-没有任何东西可以阻止它。我没有看到任何试图将工作进程同步到任何特定事件或条件的尝试。@IgorTandetnik,你能为此建议任何同步方法吗?我不能使用posix信号量(希望保持此平台独立)。一个条件变量通常与某个条件相关联;因此它的名称。您应该等待它,直到某个条件变为真(甚至还有一个wait
重载,它接受一个谓词,使这个形式化)。首先要弄清楚在每个线程可以继续之前,什么条件必须变为真(坦白地说,我不明白你想达到什么目的,所以我不能帮你)。