मैं एक सूत्र में एक समारोह के शुरू करने के लिए कोशिश कर रहा हूँ का उपयोग कर std::packaged_task
std :: packaged_task संकलन त्रुटि डब्ल्यू/जीसीसी 4.6
Query query; /* protobuf object */
/* fill Query object */
std::packaged_task<SearchResults(Query&)> task([](Query& q) ->SearchResults {
index::core::Merger merger;
return merger.search(q);
});
std::future<SearchResults> ftr = task.get_future();
std::thread(std::move(task),query).detach();
EDIT2: कोड फिर से अपडेट किया गया त्रुटियों को ठीक करने और पूर्ण त्रुटि संदेश भी शामिल थे।
जी ++ - 4.6 (उबंटू 10.04 पर) कोड संकलन करने में असमर्थ है:
In file included from /usr/include/c++/4.6/memory:80:0,
from ../src/net/QueryConnection.cpp:8:
/usr/include/c++/4.6/functional: In instantiation of ‘std::_Bind_result<void,
std::packaged_task<SearchResults(Query&)>(Query)>’:
/usr/include/c++/4.6/thread:135:9: instantiated from ‘std::thread::thread(_Callable&&,
_Args&& ...) [with _Callable = std::packaged_task<SearchResults(Query&)>, _Args =
{Query&}]’
../src/net/QueryConnection.cpp:77:36: instantiated from here
/usr/include/c++/4.6/functional:1365:7: error: ‘std::_Bind_result<_Result,
_Functor(_Bound_args ...)>::_Bind_result(const std::_Bind_result<_Result,
_Functor(_Bound_args ...)>&) [with _Result = void, _Functor =
std::packaged_task<SearchResults(Query&)>, _Bound_args = {Query},
std::_Bind_result<_Result, _Functor(_Bound_args ...)> = std::_Bind_result<void,
std::packaged_task<SearchResults(Query&)>(Query)>]’ declared to take const reference,
but implicit declaration would take non-const
Build error occurred, build is stopped
मैं पढ़ा है कि यह एक बग के कारण हो सकता है है: gcc-mailinglist
मैं सी के लिए नया हूँ ++/सी ++ 11 - एक अच्छा कामकाजी विकल्प क्या होगा? मुझे बस एक धागा लॉन्च करने की ज़रूरत है जो मुझे भविष्य देता है, जिसे get()
-method को बाद में boost::asio
एसिंक लूप में कहा जाता है।
एक समस्या यह है कि आपने पैकेज किए गए_टास्क को कोई तर्क नहीं लेने और सर्च रिसेट्स लौटने के रूप में घोषित कर दिया है, लेकिन लैम्ब्डा एक भी तर्क लेता है। मैं आपकी घोषणा 'std :: packaged_task ' –
होने की अपेक्षा करता हूं क्या आप वास्तव में पूर्ण त्रुटि संदेश पोस्ट कर सकते हैं? ऐसा लगता है कि आपने इसका केवल आधा हिस्सा पोस्ट किया है। इससे यह अनजान हो जाता है। –
मैंने लापता तर्क को दर्शाने के लिए कोड बदल दिया है और अद्यतन कोड के साथ पूर्ण त्रुटि संदेश जोड़ा है। अभी भी बहुत सारी समस्या है। –