को अवरुद्ध करने के लिए कैसे करें मेरे पास कार्य को अवरुद्ध कर दिया गया है जो find_the_question() फ़ंक्शन द्वारा किया जाएगा। हालांकि, मैं नहीं चाहता कि इस फ़ंक्शन को निष्पादित करने वाले थ्रेड को 10 सेकंड से अधिक समय लगे। तो यदि इसमें 10 सेकंड से अधिक समय लगता है, तो मैं सभी संसाधनों की सफाई के साथ उस थ्रेड को बंद करना चाहता हूं।बूस्ट थ्रेड -
मैंने इसके लिए एक कोड लिखने की कोशिश की, लेकिन किसी भी तरह से अगर मैं 10 सेकंड से अधिक समय लेता है तो मैं find_the_question() फ़ंक्शन में कोई बाधा नहीं पा रहा हूं। क्या आप कृपया मुझे बता सकते हैं कि मैं क्या गलत कर रहा हूं?
void find_the_question(std::string value)
{
//allocate x resources
try{
//do some process on resources
sleep(14);
//clean resources
}
catch(boost::thread_interrupted const&)
{
//clean resources
std::cout << "Worker thread interrupted" << std::endl;
}
}
int main()
{
boost::posix_time::time_duration timeout = boost::posix_time::milliseconds(10000);
std::cout << "In main" << std::endl;
boost::thread t1(find_the_question, "Can you block me");
t1.interrupt();
if (t1.timed_join(timeout))
{
//finished
std::cout << "Worker thread finished" << std::endl;
}
else
{
//Not finished;
std::cout << "Worker thread not finished" << std::endl;
}
std::cout << "In main end" << std::endl;
}
आउटपुट: t1 पूरा करने के लिए 10 सेकंड से अधिक लेता है हैं, मैं कंसोल आउटपुट निम्नलिखित हो रही है।
std::cout << "In main" << std::endl;
std::cout << "Worker thread not finished" << std::endl;
std::cout << "In main end" << std::endl;
जबकि, मैं उत्पादन
std::cout << "In main" << std::endl;
std::cout << "Worker thread interrupted" << std::endl;
std::cout << "Worker thread not finished" << std::endl;
std::cout << "In main end" << std::endl;
आप मुझे बता सकते हैं क्या मैं गलत कर रहा हूँ निम्नलिखित की उम्मीद कर रहा हूँ।
अग्रिम
'नींद (14);' जिसका आप उपयोग कर रहे हैं। क्या यह 'बूस्ट :: थ्रेड' से नींद है या क्या यह सी नींद का काम है? क्योंकि बाधा केवल 'बूस्ट :: थ्रेड: नींद' पर काम करती है – mkaes