多个线程可以加入同一个提升::线程吗?
Can multiple threads join the same boost::thread?
如果多个线程尝试加入同一线程,pthreads
具有未定义的行为:
如果多个线程同时尝试与同一线程连接, 结果未定义。
boost::thread
也是如此吗?文档似乎没有指定这一点。
如果未定义,那么多个线程等待一个线程完成的干净方法是什么?
如果未定义,那么多个线程等待一个线程完成的干净方法是什么?
干净的方法是让一个线程通知其他线程它是完整的。packaged_task
包含一个可以等待的future
,这可以在这里帮助我们。
这是一种方法。我使用了std::thread和std::p ackaged_task,但你也可以使用boost等效物。
#include <thread>
#include <mutex>
#include <future>
#include <vector>
#include <iostream>
void emit(const char* msg) {
static std::mutex m;
std::lock_guard<std::mutex> l(m);
std::cout << msg << std::endl;
std::cout.flush();
}
int main()
{
using namespace std;
auto one_task = std::packaged_task<void()>([]{
emit("waiting...");
std::this_thread::sleep_for(std::chrono::microseconds(500));
emit("wait over!");
});
// note: convert future to a shared_future so we can pass it
// to two subordinate threads simultaneously
auto one_done = std::shared_future<void>(one_task.get_future());
auto one = std::thread(std::move(one_task));
std::vector<std::thread> many;
many.emplace_back([one_done] {
one_done.wait();
// do my thing here
emit("starting thread 1");
});
many.emplace_back([one_done] {
one_done.wait();
// do my thing here
emit("starting thread 2");
});
one.join();
for (auto& t : many) {
t.join();
}
cout << "Hello, World" << endl;
return 0;
}
预期输出:
waiting...
wait over!
starting thread 2
starting thread 1
Hello, World
我最终使用了boost::condition_variable
...大约:
class thread_wrapper {
boost::mutex mutex;
boost::condition_variable thread_done_condition;
bool thread_done = false;
void the_func() {
// ...
// end of the thread
{
boost:unique_lock<boost::mutex> lock(mutex);
thread_done = true;
}
thread_done_condition.notify_all();
}
void wait_until_done() {
boost::unique_lock<boost::mutex> lock(mutex);
thread_done_condition.wait(lock, [this]{ return thread_done; });
}
}
然后,多个呼叫者可以安全地呼叫wait_until_done()
。
现在我感到震惊的是,类似以下内容也可以工作:
class thread_wrapper {
public:
thread_wrapper() : thread([this]() { this->the_func(); }) { }
void wait_until_done() {
boost::unique_lock<boost::mutex> lock(join_mutex);
thread.join();
}
private:
void the_func() {
// ...
}
boost::mutex join_mutex;
boost::thread thread;
}
相关文章:
- 为什么std::async使用同一个线程运行函数
- cuda:多个线程访问同一个全局变量
- 多个线程可以读取同一个类成员变量吗?
- 多个线程访问同一个 cuda 流
- 如果两个线程调用同一个函数,但函数中的所有变量都是局部变量,我还需要担心线程之间共享数据吗?
- 同一个ifstream可以有多个"cursors"吗?这会是线程安全的吗?
- 将两个线程同步到同一个计时器
- fopen 和 fwrite 从多个线程到同一个文件
- 如何在同一个线程上用同一个互斥对象锁定两次
- 通过同一个ssl连接从多个线程发送请求
- 多个线程无法访问同一个指针而不会出错
- 多个线程可以加入同一个提升::线程吗?
- cv::VideoCapture:从两个线程访问同一个网络摄像头
- 创建了三个线程来实例化同一个类.但很快只有一个线程继续运行
- 两个线程(COM和WPF UI)访问同一个浮点
- 两个线程可以同时从同一个QList中读取
- 为什么要将类的线程函数设置为静态,以便在同一个类中可以访问
- 在同一个cpu内核上执行的OpenMP线程
- QThread的多个实例可以引用同一个操作系统线程吗?
- openmp是否可以在同一个cpu(核心)上运行不同的线程?