我想知道是否可以使用结构作为承诺的返回类型。我查看了C++引用,没有提到对允诺类型的限制。我可以找到的所有示例都使用了INT、Long、Double、String类型,但从不使用结构。我修改了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;
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。