Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6686769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:12:37+00:00 2026-05-26T05:12:37+00:00

i would like to add a callback function to a threaded function without freezing

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:12:38+00:00Added an answer on May 26, 2026 at 5:12 am

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to do something like add a nice-to-Excel-functions Name property to the
I would like to add the following MIME type to a site run by
I would like to add a DataGridViewTextBoxCell cell to a DataGridViewCell control, but as
I would like to add AES encryption to a software product, but am concerned
I would like to add graphing to my User Controls in ASP.NET MVC. I
I would like to add a typing speed indicator just below the textarea we
I would like to add a BuildListener to my headless build process, which is
I would like to add some icons to my treeview. Is there a way
I would like to add a method to a built-in type (e.g. Double), so
I would like to add OpenId support to an app. It runs on ASP.NET

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.