C++ 多线程应用程序没有输出
我正在做一个处理多线程的应用程序来进行练习。假设我们有10辆车,并且有一个停车场,最多可以容纳5辆车。如果一辆车不能停车,它会等到有空余空间时再停车。C++ 多线程应用程序没有输出,c++,multithreading,c++11,C++,Multithreading,C++11,我正在做一个处理多线程的应用程序来进行练习。假设我们有10辆车,并且有一个停车场,最多可以容纳5辆车。如果一辆车不能停车,它会等到有空余空间时再停车。 我使用c++11线程执行此操作: #include <iostream> #include <thread> #include <mutex> #include <condition_variable> using namespace std; int cars=0; int max_cars=
我使用c++11线程执行此操作:
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
using namespace std;
int cars=0;
int max_cars=5;
mutex cars_mux;
condition_variable cars_cond;
bool pred()
{
return cars< max_cars;
}
void task()
{
unique_lock<mutex> lock(cars_mux);
while(true)
{
cars_mux.lock();
cars_cond.wait(lock,pred);
cars++;
cout << this_thread::get_id() << " has parked" << endl;
cars_mux.unlock();
this_thread::sleep_for(chrono::seconds(1)); // the cars is parked and waits some time before going away
cars_mux.lock();
cars--;
cars_cond.notify_one();
cars_mux.unlock();
}
}
int main(int argc, char** argv)
{
thread t[10];
for(int i=0; i<10; i++)
t[i]=thread(task);
for(int i=0; i<10; i++)
t[i].join();
return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int cars=0;
int max_cars=5;
多路复用器;
条件可变车辆条件;
布尔pred()
{
返回车<最大车数;
}
无效任务()
{
独特的锁(车锁);
while(true)
{
汽车多路锁();
车辆第二次等待(锁定、预定);
汽车++;
cout这里有两个问题:
首先,当您构造锁
对象时
unique_lock<mutex> lock(cars_mux);
其次,线程无法加入,因为无法退出任务中的while(true)
循环-汽车将永远停驻!您根本不需要while
循环
如果您删除第一个cars_mux.lock();
,在while
循环结束时相应的解锁尝试,以及while
循环本身,您应该会得到所需的行为。弗雷泽的回答很好,但当我看到这个时,我觉得一个工作示例会很好
我做了一些改变:
- 就我个人而言,我喜欢使用RAII来锁定/解锁,这样你就不会忘记这样做(即使这意味着额外的作用域)。在过去,我也有一些我无法理解的比赛条件,而切换到RAII方法通常也会使这些条件消失……这更简单,所以就这样做吧;)
- 我喜欢看到汽车何时离开,所以我也为此添加了I/O
下面是所述问题的工作代码示例。仅供参考,我将clang 3.1与libc++结合使用:
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <chrono>
using namespace std;
int cars=0;
int max_cars=5;
mutex cars_mux;
condition_variable cars_cond;
bool pred()
{
return cars < max_cars;
}
void task()
{
{
unique_lock<mutex> carlock(cars_mux);
cars_cond.wait(carlock,pred);
cars++;
cout << "Thread " << this_thread::get_id()
<< " has parked. There are " << cars << " parked cars." << endl;
}
this_thread::sleep_for(chrono::seconds(1));
{
unique_lock<mutex> carlock(cars_mux);
cars--;
cout << "Thread " << this_thread::get_id()
<< " has left. There are " << cars << " parked cars." << endl;
cars_cond.notify_one();
}
}
int main(int argc, char** argv)
{
const int NumThreads = 10;
thread t[NumThreads];
for(int i=0; i<NumThreads; i++)
t[i]=thread(task);
for(int i=0; i<NumThreads; i++)
t[i].join();
return 0;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int cars=0;
int max_cars=5;
多路复用器;
条件可变车辆条件;
布尔pred()
{
返回车<最大车数;
}
无效任务()
{
{
独特的锁具(汽车);
汽车二等(卡洛克,普雷德);
汽车++;
除了弗雷泽的好建议之外,你应该使用唯一锁来锁定/解锁你的互斥锁,而不是直接使用互斥锁:lock.lock()
和lock.unlock()
。这将确保如果在循环中抛出异常,lock
将在异常传播出去时做正确的事情,因为它将知道cars\u mux
是否被锁定。输出总是在锁定互斥体时发生,我看不出为I/O设置互斥体的原因。我尝试了弗雷泽的建议和code完成了任务。@RamyAlZuhouri你完全正确。我把事情复杂化了,所以我编辑了代码,把它简化了。
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <chrono>
using namespace std;
int cars=0;
int max_cars=5;
mutex cars_mux;
condition_variable cars_cond;
bool pred()
{
return cars < max_cars;
}
void task()
{
{
unique_lock<mutex> carlock(cars_mux);
cars_cond.wait(carlock,pred);
cars++;
cout << "Thread " << this_thread::get_id()
<< " has parked. There are " << cars << " parked cars." << endl;
}
this_thread::sleep_for(chrono::seconds(1));
{
unique_lock<mutex> carlock(cars_mux);
cars--;
cout << "Thread " << this_thread::get_id()
<< " has left. There are " << cars << " parked cars." << endl;
cars_cond.notify_one();
}
}
int main(int argc, char** argv)
{
const int NumThreads = 10;
thread t[NumThreads];
for(int i=0; i<NumThreads; i++)
t[i]=thread(task);
for(int i=0; i<NumThreads; i++)
t[i].join();
return 0;
}