i would like to add a callback function to a threaded function without freezing the main application.
Ex: when I click on a button, it start a threaded function. I wanna inform the user when the work is finish.
Thx
cs functions;
pthread_t thread;
pthread_create(&thread, NULL, maFonction, (void*)&functions);
//pthread_join(thread, NULL);
The pthread_join block the main application when waiting for the thread to finish. So how would I do it. Thx a lot
Make the thread in a detached state by calling
pthread_detach()in the spawned thread, or when creating the thread in the main thread, set the pthread attributes for that thread to a detached state. Now that the thread is detached, you won’t need to callpthread_join()in the main thread. Next, in the spawned thread itself, before exiting the thread, push an event onto event queue of the WxWidgets object that spawned the thread in order to “announce” that the spawned thread has completed. Finally, add an event handler for your thread-finishing event to your WxWidget object to handle the even the spawned thread will place on it’s event queue.For instance, you could create an event like
THREAD_FINISHED_EVENTthat you thread will push onto the event-queue of the object that will spawn the threads. You code would look like the following:The event itself will be processed in the main event thread of the WxWidget that installs the handler for the event. You’ll just need to provide a handler for the WxWidget object, and define the event itself. This can be done using the macro
DEFINE_EVENT_TYPE, and then adding the following line to the constructor of the WxWidget object that will be spawning the threads themselves:Summing this all up, here’s what some theoretical WxWidgets object class would look like: