现代C++.从继续执行的工作线程返回数据结构
Modern C++. Return data structure from working thread continuing its execution
我需要启动工作线程,执行一些初始化,返回数据结构作为初始化结果并继续线程执行。仅使用现代 c++ 功能来实现此目的的最佳(或可能)代码是什么?请注意,启动的线程应继续执行(线程不会像往常一样终止)。遗憾的是,大多数解决方案都假定工作线程终止。
伪代码:
// Executes in WorkerThread context
void SomeClass::Worker_treadfun_with_init()
{
// 1. Initialization calls...
// 2. Pass/signal initialization results to caller
// 3. Continue execution of WorkerThread
}
// Executes in CallerThread context
void SomeClass::Caller()
{
// 1. Create WorkerThread with SomeClass::Worker_treadfun_with_init()" thread function
// 2. Sleep thread for some initialization results
// 3. Grab results
// 3. Continue execution of CallerThread
}
我认为std::future符合您的要求。
// Executes in WorkerThread context
void SomeClass::Worker_treadfun_with_init(std::promise<Result> &pro)
{
// 1. Initialization calls...
// 2. Pass/signal initialization results to caller
pro.set_value(yourInitResult);
// 3. Continue execution of WorkerThread
}
// Executes in CallerThread context
void SomeClass::Caller()
{
// 1. Create WorkerThread with SomeClass::Worker_treadfun_with_init()" thread function
std::promise<Result> pro;
auto f=pro.get_future();
std::thread([this,&pro](){Worker_treadfun_with_init(pro);}).detach();
auto result=f.get();
// 3. Grab results
// 3. Continue execution of CallerThread
}
尝试使用指针或对包含答案的数据结构的引用,以及 std::condition_variable 以在计算答案时通知您:
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <chrono>
#include <vector>
std::vector<double> g_my_answer;
std::mutex g_mtx;
std::condition_variable g_cv;
bool g_ready = false;
void Worker_treadfun_with_init()
{
//Do your initialization here
{
std::unique_lock<std::mutex> lck( g_mtx );
for( double val = 0; val < 10; val += 0.3 )
g_my_answer.push_back( val );
g_ready = true;
lck.unlock();
g_cv.notify_one();
}
//Keep doing your other work..., here we'll just sleep
for( int i = 0; i < 100; ++i )
{
std::this_thread::sleep_for( std::chrono::seconds(1) );
}
}
void Caller()
{
std::unique_lock<std::mutex> lck(g_mtx);
std::thread worker_thread = std::thread( Worker_treadfun_with_init );
//Calling wait will cause current thread to sleep until g_cv.notify_one() is called.
g_cv.wait( lck, [&g_ready](){ return g_ready; } );
//Print out the answer as the worker thread continues doing its work
for( auto val : g_my_answer )
std::cout << val << std::endl;
//Unlock mutex (or better yet have unique_lock go out of scope)
// incase worker thread needs to lock again to finish
lck.unlock();
//...
//Make sure to join the worker thread some time later on.
worker_thread.join();
}
当然,在实际代码中,您不会使用全局变量,而是通过指针或引用(或作为 SomeClass
的成员变量)将它们传递给工作线程函数,但您明白了这一点。
相关文章:
- 工作线程在执行太快后永久休眠
- ZeroMQ 在使用 std::thread 创建工作线程时崩溃
- 工作线程一直在等待,condition_variable甚至调用了notify_all
- 使用 std::atomic 标志和 std::condition_variable 在工作线程上等待
- Qt-工作线程崩溃时将cv::Mat转换为QImage
- 无法将接口从主线程封送到工作线程
- 在 C++ 中扩展作业/工作线程多线程系统
- Qt C++ - 如何将数据从工作线程传递到主线程?
- 从线程池工作线程使用 GetQueuedCompletionStatus 的奇怪行为
- QtThread:I/O 队列的工作线程
- 将信号从工作线程类连接到控制器类 - QThreads
- Qt:工作线程和 GUI 事件之间的关系
- 将数据集几乎平均分配给工作线程
- 在为工作线程访问 lambda 中捕获的向量列表中的元素引用时,是否需要互斥锁?
- 如何将C++ dll 在 C# 窗口窗体应用程序下的工作线程中运行
- 在Qt中使用工作线程将数据写入文件的正确方法是什么?
- 在工作线程中使用 QT 主窗口
- C++,pthreads:如何从多个线程停止工作线程
- 同步主线程和工作线程
- 从工作线程更新QtCharts的正确方法