C++11 std::线程并等待线程完成
C++11 std::threads and waiting for threads to finish
我有一个计时器对象的向量。每个计时器对象都会启动一个模拟生长周期的 std::thread。我正在使用命令模式。
正在发生的事情是每个计时器一个接一个地被执行,但我真正想要的是执行一个......然后一旦完成,下一个...一旦完成下一个...同时不干扰程序的主要执行
class Timer
{
public:
bool _bTimerStarted;
bool _bTimerCompleted;
int _timerDuration;
virtual ~Timer() { }
virtual void execute()=0;
virtual void runTimer()=0;
inline void setDuration(int _s) { _timerDuration = _s; };
inline int getDuration() { return _timerDuration; };
inline bool isTimerComplete() { return _bTimerCompleted; };
};
class GrowingTimer : public Timer
{
public:
void execute()
{
//std::cout << "Timer execute..." << std::endl;
_bTimerStarted = false;
_bTimerCompleted = false;
//std::thread t1(&GrowingTimer::runTimer, this); //Launch a thread
//t1.detach();
runTimer();
}
void runTimer()
{
//std::cout << "Timer runTimer..." << std::endl;
_bTimerStarted = true;
auto start = std::chrono::high_resolution_clock::now();
std::this_thread::sleep_until(start + std::chrono::seconds(20));
_bTimerCompleted = true;
std::cout << "Growing Timer Finished..." << std::endl;
}
};
class Timers
{
std::vector<Timer*> _timers;
struct ExecuteTimer
{
void operator()(Timer* _timer) { _timer->execute(); }
};
public:
void add_timer(Timer& _timer) { _timers.push_back(&_timer); }
void execute()
{
//std::for_each(_timers.begin(), _timers.end(), ExecuteTimer());
for (int i=0; i < _timers.size(); i++)
{
Timer* _t = _timers.at(i);
_t->execute();
//while ( ! _t->isTimerComplete())
//{
//}
}
}
};
执行上述操作,例如:
Timers _timer;
GrowingTimer _g, g1;
_g.setDuration(BROCCOLI::growTimeSeconds);
_g1.setDuration(BROCCOLI::growTimeSeconds);
_timer.add_timer(_g);
_timer.add_timer(_g1);
start_timers();
}
void start_timers()
{
_timer.execute();
}
在计时器::执行中,我正在尝试几种不同的方法来执行第一个而不是执行接下来,直到我以某种方式发出信号,它已经完成。
更新:
我现在这样做是为了执行所有内容:
Timers _timer;
GrowingTimer _g, g1;
_g.setDuration(BROCCOLI::growTimeSeconds);
_g1.setDuration(BROCCOLI::growTimeSeconds);
_timer.add_timer(_g);
_timer.add_timer(_g1);
//start_timers();
std::thread t1(&Broccoli::start_timers, this); //Launch a thread
t1.detach();
}
void start_timers()
{
_timer.execute();
}
第一次完成(我看到"完成"的cout(,但在for loop
内_t->execute();
崩溃,EXEC_BAD_ACCESS。我添加了一个 cout 来检查矢量的大小,它是 2,所以两个计时器都在里面。我确实在控制台中看到了这一点:
this Timers * 0xbfffd998
_timers std::__1::vector<Timer *, std::__1::allocator<Timer *> >
如果我将detach()
更改为join()
一切都在没有崩溃的情况下完成,但它会阻止我的应用程序的执行,直到这些计时器完成。
你为什么在这里使用线程? Timers::execute()
在计时器上调用execute
,然后等待它完成,然后在下一个计时器上调用execute
,依此类推。为什么不直接在Timers::execute()
中调用计时器函数,而不是生成一个线程然后等待它?
线程允许您编写并发执行的代码。你想要的是串行执行,所以线程是错误的工具。
更新:在更新的代码中,您在后台线程上运行start_timers
,这很好。但是,通过分离该线程,会使线程运行超过作用域的末尾。这意味着计时器对象_g
和_g1
,甚至Timers
对象_timers
都可能在线程完成之前被销毁。鉴于计时器线程的耗时性质,以及为了避免代码阻塞而使用detach
而不是join
的事实,这肯定是导致问题的原因。
如果在线程上运行代码,则需要确保该线程访问的所有对象的生存期足够长,以便在线程访问它们时它们仍然有效。对于分离的线程,这尤其难以实现,因此不建议使用分离的线程。
一种选择是创建一个包含_timers
的对象,_g
和_g1
在线程t1
旁边,并使其析构函数与线程连接。然后,您需要做的就是确保对象一直存在,直到可以安全地等待计时器完成为止。
如果你不想干扰程序的执行,你可以做一些@Joel说的事情,但也在 Timers 类中添加一个线程,这将执行向量中的线程。
您可以在GrowingTimer
中包含对thread
的unique_ptr
,而不是在execute
中将其创建为本地对象并调用detach
。您仍然可以在 execute
中创建线程,但您可以通过unique_ptr::reset
调用来完成。
然后使用 join
而不是 isTimerComplete
(将 join
函数添加到Timer
基类(。isTimerComplete
轮询机制的效率极低,因为它基本上会用完该线程的整个时间片,不断轮询,而join
会阻塞,直到另一个线程完成。
join
的例子:
#include <iostream>
#include <chrono>
#include <thread>
using namespace std;
void threadMain()
{
this_thread::sleep_for(chrono::seconds(5));
cout << "Done sleepingn";
}
int main()
{
thread t(threadMain);
for (int i = 0; i < 10; ++i)
{
cout << i << "n";
}
t.join();
cout << "Press Enter to exitn";
cin.get();
return 0;
}
注意主线程如何保持运行,而另一个线程执行其操作。请注意,Anthony 的回答是正确的,因为您似乎并不需要多个后台线程来按顺序执行任务,而不是启动线程并等待它完成,然后再开始一个新线程。
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 提升 ASIO - io_service 不要等待连接到线程
- 虚假唤醒是否会解锁所有等待线程,甚至是不相关的线程?
- 一个线程等待多个线程事件
- 如何让线程等待对象完全破坏?(对象也有一个线程)?
- 停止C++ 11 个 std::线程等待 std::condition_variable
- C++线程:等待condition_variable后无法解锁阵列中的互斥锁
- Qt 线程等待来自 GUI 的输入
- C++11线程等待
- 如何让线程等待对象的销毁
- Boost:等待工作线程等待条件变量
- C++11线程等待行为:std::this_Thread::yield()与std::this _Thread::sle
- 如何使线程等待而不轮询
- 使其中一个线程等待的时间尽可能少(几乎为零)
- 当线程等待互斥锁时捕获的信号
- Qt:如何让一个线程等待临时包障,并暂时增加另一个线程的优先级以删除 roadbock?
- C++ Windows MFC 并发:让线程等待,直到达到特定状态
- 线程等待父线程
- 使用异步方法vs线程等待
- c++线程等待时间