标签云

微信群

扫码加入我们

WeChat QR Code

I would like to use threads in a switch.Is this possible or is there another way to interrupt the thread elsewhere in the program?switch (event_element)// dispatch event handler{case 1: // main task thread{boost::thread thr_check_db_task (check_db_task);break;}case 2:{std::cerr << "DATABASE CONNECTION ERROR" << std::endl;thr_check_db_task.interrupt ();// **COMPILE ERROR**mysql_connection_error ();break;}default:break;}Thank you.


You are accessing an out-of-scope local variable thr-check-db_task. That's why you got an error. One way is to keep it as a member variable of the class.

2019年04月18日09分46秒

You didn't post the compiler error you're getting, so I'll guess that its something along the lines of"thr_check_db_task not declared in this scope"? If it is, then it makes sense.. the_check_db_task is clearly not in the scope of case 2.

2019年04月18日09分46秒

Yes, you are both right.How can I start a thread and interrupt it asynchronously?

2019年04月18日09分46秒

xinthose See my answer.

2019年04月19日09分46秒

thank you; how would I use this from int main exactly?My switch is in int main.

2019年04月19日09分46秒

You can use either a pointer to a heap-allocated thread object, or use the swap/move approach. You make a NULL pointer or a default-constructed object prior to the switch.

2019年04月18日09分46秒