承诺可以使用结构类型吗?
Can a promise use a struct type?
我想知道是否可以使用结构作为承诺的返回类型。我查看了C++参考资料,没有说明对承诺类型的限制。我能找到的所有示例都使用 int、long、double、字符串类型的 promise,但从不使用结构。我修改了在Microsoft(TM(网站上找到的示例,我对其进行了轻微修改(代码,而不是网站(。它应该有效,但我一直遇到此错误:
k.cpp: In function ‘void DoSomeWork(int, std::promise<toBeReturned>&)’:
k.cpp:25:9: error: ‘class std::promise<toBeReturned>’ has no member named ‘a’
ret.a.set_value(it);
^
compilation terminated due to -Wfatal-errors.
看不出我是否犯了一个不可原谅的新手错误,或者我是否遇到了一些我不明白的事情。
此致敬意
司仪
这是代码---------------------------------------------
#include <chrono>
#include <iostream> // std::cout
#include <functional> // std::ref
#include <thread> // std::thread
#include <future> // std::promise, std::future
using namespace std;
using namespace std::chrono;
struct toBeReturned
{
int a;
int b;
int c;
};
void DoSomeWork(int numIters, promise<toBeReturned>& ret)
{
int it = 0;
for ( ; it < numIters; ++it)
{
cout << "Thread " << this_thread::get_id() << " working..." << endl;
this_thread::sleep_for(milliseconds(1000));
}
ret.a.set_value(it);
ret.b.set_value(it*10);
ret.c.set_value(it*100);
}
void TestPromise()
{
int its = 10;
int i=0;
bool notDoneYet=true;
promise<int> myPromise;
future<int> myFuture = myPromise.get_future();
thread aThread(DoSomeWork, its, ref(myPromise));
std::future_status myFutureStatus;
while(notDoneYet)
{
i++;
try
{
myFutureStatus=myFuture.wait_for(milliseconds(100));
if(myFuture.valid() && myFutureStatus != future_status::ready)
{
cout << "Thread " << this_thread::get_id() << " waiting..." << endl;
}
else if(myFuture.valid() && myFutureStatus == std::future_status::ready)
{
toBeReturned valuesToBeReturned;
try
{
valuesToBeReturned=myFuture.get();
cout << "Work iterations -> a " << a << " b " << b << " c " << c << endl;
aThread.join();
notDoneYet=false;
}
catch (std::future_error& e)
{
cout << "(2) future_error caught: " << e.code().message() << endl;
}
}
}
catch (future_error& e)
{
cout << "(1) future_error caught: " << e.code().message() << endl;
}
}
}
int main()
{
TestPromise();
return 0;
}
非常感谢您清晰而有用的答案。事实上,这是一个完全的类型不匹配混乱。
这是更正后的代码:
#include <chrono>
#include <iostream> // std::cout
#include <functional> // std::ref
#include <thread> // std::thread
#include <future> // std::promise, std::future
using namespace std;
using namespace std::chrono;
struct toBeReturned
{
int a;
int b;
int c;
};
void DoSomeWork(int numIters, promise<toBeReturned>& ret)
{
int it = 0;
toBeReturned tmp; // **********************
for ( ; it < numIters; ++it)
{
cout << "Thread " << this_thread::get_id() << " working..." << endl;
this_thread::sleep_for(milliseconds(1000));
}
tmp.a=it; // **********************
tmp.b=it*10; // **********************
tmp.c=it*100; // **********************
ret.set_value(tmp); // **********************
}
void TestPromise()
{
int its = 10;
int i=0;
bool notDoneYet=true;
promise<toBeReturned> myPromise; // **********************
future<toBeReturned> myFuture = myPromise.get_future(); // **********************
thread aThread(DoSomeWork, its, ref(myPromise));
std::future_status myFutureStatus;
while(notDoneYet)
{
i++;
try
{
myFutureStatus=myFuture.wait_for(milliseconds(100));
if(myFuture.valid() && myFutureStatus != future_status::ready)
{
cout << "Thread " << this_thread::get_id() << " waiting..." << endl;
}
else if(myFuture.valid() && myFutureStatus == std::future_status::ready)
{
toBeReturned valuesToBeReturned;
try
{
valuesToBeReturned=myFuture.get();
cout << "Work iterations -> a " << valuesToBeReturned.a << " b " << valuesToBeReturned.b << " c " << valuesToBeReturned.c << endl; // **********************
aThread.join();
notDoneYet=false;
}
catch (std::future_error& e)
{
cout << "(2) future_error caught: " << e.code().message() << endl;
}
}
}
catch (future_error& e)
{
cout << "(1) future_error caught: " << e.code().message() << endl;
}
}
}
int main()
{
TestPromise();
return 0;
}
相关文章:
- 没有类型结构绑定不起作用?
- 如何使用指针连接两种不同的节点类型(结构)?
- 无法汇总初始化类型结构的变量,其中包含在类模板中的位字段成员
- 您如何声明类型结构的堆栈?在C 中
- 访问指向相同类型结构的指针
- 静态数据类型(结构)的组织
- 创建类型结构的指针阵列
- 处理类型结构数组
- 嵌入式 C++:在 C++ 中初始化易失性类型结构
- Mac OSX的c++中的变量类型结构(oaidl.h)
- 嵌套类型:结构与类
- 如何在 python 中实现 c++ 类型结构
- 如何使用类型结构定义双端队列(双面队列)
- 删除类型结构的元素形式向量
- 从类型结构的向量中删除元素
- 在c#、c++和java中创建python弱类型结构的强类型版本
- 从较大的数据类型(结构)转换为较小的数据类型
- 使用QGLShaderProgram从Qt传递自定义类型(结构体)统一到GLSL
- 如何:公开c++数据结构/类型(结构体,枚举)供c#使用
- 按值将类型结构传递给函数,错误:声明"average"中的两个或多个数据类型!!-