承诺可以使用结构类型吗?

Can a promise use a struct type?

本文关键字:类型 结构 可以使 承诺      更新时间:2023-10-16

我想知道是否可以使用结构作为承诺的返回类型。我查看了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;
}