मैं काफी आसान होना चाहिए क्या करना चाहते हैं, लेकिन मैं इसे नहीं मिलता है क्या ...उपयोग सदस्य समारोह :: packaged_task
सभी मैं करना चाहता हूँ में एक वर्ग के एक सदस्य समारोह शुरू करने के लिए है समय में कुछ निश्चित बिंदु पर पृष्ठभूमि । उस समारोह का नतीजा भी "बाहरी रूप से" उपलब्ध होना चाहिए। तो मैं कन्स्ट्रक्टर में कार्य तैयार करना चाहता हूं (भविष्य परिवर्तनीय सेट कर रहा हूं ...) और इसे बाद के बिंदु पर शुरू करना चाहता हूं।
मैंने std को गठबंधन करने की कोशिश की :((packaged_task | async | भविष्य) लेकिन मुझे इसे काम नहीं मिला।
यह स्निपेट संकलन नहीं होगा, लेकिन मैं यह पता चलता है कि मैं क्या करना चाहते हैं लगता है: पहले से
class foo {
private:
// This function shall run in background as a thread
// when it gets triggered to start at some certain point
bool do_something() { return true; }
std::packaged_task<bool()> task;
std::future<bool> result;
public:
foo() :
task(do_something), // yes, that's wrong, but how to do it right?
result(task.get_future())
{
// do some initialization stuff
.....
}
~foo() {}
void start() {
// Start Task as asynchron thread
std::async as(std::launch::async, task); // Also doesn't work...
}
// This function should return the result of do_something
bool get_result() { return result.get(); }
};
धन्यवाद! इसके अलावा
#include <functional> // For std::bind()
foo() :
task(std::bind(&foo::do_something, this)),
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
result(task.get_future())
{
// ...
}
, आप यहाँ गलत बात कर रहे हैं:
'std :: async' एक कार्य है, एक प्रकार –