multithreading - Can not start threads from pointer to function, C++ -
i trying start thread execution doing following:
#include <thread> #include <stdio.h> typedef void (*callback_function)(void); void printer(){ printf("something\n"); } void dotask(callback_function a){ std::thread t1(a); } int main(){ callback_function print = printer; dotask(print); } executing piece of code, result core dumped. surprisingly, when change function dotask one:
void dotask(callback_function a){ a(); } it works, or doing works:
int main(){ callback_function print = printer; std::thread t1(printer); } does knows i'm missing or doing wrong?
both cases wrong. object std::thread t1 lives until function dotask exits. thread object trying destroying while callback_function still works.
should wait thread stop it's work , delete it.
int main(){ callback_function print = printer; std::thread t1(printer); // other stuff t1.join(); }
Comments
Post a Comment