响应式营销型网站建设,wordpress添加自定义链接,软件工程师中级职称,用php做购物网站视频引言
关于线程处理函数#xff0c;常见的可能是返回值为void类型#xff0c;那线程处理函数是否能定义自己想要的返回值类型呢#xff0c;这里请看下面的说明。
C线程返回值
应用环境1、传统的方式获取线程返回值2、使用C Promise和future方式3、promise和future介绍 应…引言
关于线程处理函数常见的可能是返回值为void类型那线程处理函数是否能定义自己想要的返回值类型呢这里请看下面的说明。
C线程返回值
应用环境1、传统的方式获取线程返回值2、使用C Promise和future方式3、promise和future介绍 应用环境
通常主线程某些任务需要通过子线程完成数据处理之后才能继续执行传统的方式就是主线程标志位给到子线程去然后主线程这边采用循环等待的方式去等标志是否已经完成标示。另外一种方式就是使用c11 promis 和future来完成 1、传统的方式获取线程返回值
代码如下
#include iostream
#include thread
#include future
#include QDebug
using namespace std;
void thread_old(bool pRet)
{
qDebug() QString(“thread_old is called.”) “\n”;
pRet true;
}void main()
{
// use the old way
bool bReturn false;
thread threadOld(thread_old, bReturn);
threadOld.detach();
while (true)
{
if (bReturn)
{
qDebug() QString(“thread_old is return”) “\n”;
break;
}
}
}输出结果如下
2、使用C Promise和future方式
代码如下
#include iostream
#include thread
#include future
#include QDebug
using namespace std;
void thread_new(promisebool *bPromise)
{
qDebug() QString(“thread_new is called.”) “\n”;
bPromise-set_value(true);
}void main()
{
// the new way
promisebool bPromise;
futurebool bFuture bPromise.get_future();
thread threadNew(thread_new, bPromise);
threadNew.detach();
qDebug() QString(“future val:%1”).arg(bFuture.get()) “\n”;
}输出结果如下 3、promise和future介绍
std::future是一个类模板(class template)其对象存储未来的值一个std::future对象在内部存储一个将来会被赋值的值并提供了一个访问该值的机制通过get()成员函数实现。在此之前试图在get()函数可用之前通过它来访问相关的值那么get()函数将会阻塞直到该值可用。std::promise也是一个类模板其对象有可能在将来会被人为赋值每个std::promise对象有一个对应的std::future对象一旦由std::promise对象设置std::future将会对其赋值。std::promise对象与其管理的std::future对象共享数据。 总结
结合这篇文章可以看出通过参数可以将想要的值获取至于能否通过指定返回值类型返回想要的数据这里还是不太清楚应该是不可以。还是采用参数传回想要的数据吧。