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 开发者_JAVA百科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 call pthread_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_EVENT
that you thread will push onto the event-queue of the object that will spawn the threads. You code would look like the following:
wxCommandEvent event(THREAD_FINISHED_EVENT, GetId());
//"this" points to the parent WxWidgets object spawning the threads
//and allows you to access the "this" pointer in the handler
event.SetEventObject(this);
//Send the event
this->AddPendingEvent(event);
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:
//myWxWidget::thread_handler is the handler for your thread ending events
Connect(widgetID, THREAD_FINISHED_EVENT, wxCommandEventHandler(myWxWidget::thread_handler))
Summing this all up, here's what some theoretical WxWidgets object class would look like:
//myWindowThreadClass.hpp
#include <wx/wx.h>
#include <wx/event.h>
extern expdecl const wxEventType THREAD_FINISHED_EVENT;
class myWindowThreadClass: public wxWindow
{
public:
myWindowThreadClass(wxWindow* parent, int id);
//handler for the thread-ending event
void thread_handler(wxCommandEvent& event);
//pushes a thread event on the wxWidgets event-queue
//for myWindowThreadClass
void send_thread_event();
};
//myWindowThreadClass.cpp
#include <myWindowthreadClass.h>
#include <pthread.h>
const wxEventType THREAD_FINISHED_EVENT = wxNewEventType();
void* thread_func(void* data)
{
myWindowThreadClass* window_ptr = static_cast<myWindowThreadClass*>(data);
//detach thread
pthread_detatch(pthread_self());
//... rest of thread function
window_ptr->send_thread_event();
return (void*)0;
}
myWindowThreadClass::myWindowThreadClass(wxWindow* parent, int id):
wxWindow(parent, id)
{
//enable the event handler
Connect(id, THREAD_FINISHED_EVENT, wxCommandEventHandler(myWindowThreadClass::thread_handler));
//create your threads
pthread_t tid;
for (int i=0; i < NUM_THREADS; i++)
{
pthread_create(&tid, NULL, thread_func, this);
}
//...do anything else needed to initialize object
}
void myWindowThreadClass::thread_handler(wxCommandEvent& event)
{
//handle the event
}
void myWindowThreadClass::send_thread_event()
{
wxCommandEvent event(THREAD_FINISHED_EVENT, GetId());
event.SetEventObject(this);
//Send the event ... import to use this function, as it will cause
//the event to be processed in main-thread, not spawned child threads
this->AddPendingEvent(event);
}
Set a variable, eg. xxx_is_done
, to false
before starting the thread. And when the thread is done, the last thing it does is set xxx_is_done
to true
. Then just check the variable in the main event loop and call pthread_join
on the thread when the variable is true and also set the variable back to false
so you don't call pthread_join
on the thread again.
精彩评论