I have a program with a button that calls a function that can take some
time to finish (up to 10 seconds). I need the main thread to not be blocked
while this function finish so that the GUI remain responsive. I thought I
could use threads since even though IUP is not thread safe, I didn’t need
to update the interface from within the worker function.
I have near cero experience with multi-thread libraries but I tried with
pthread. So I created a new thread from the callback, this second thread
would run the worker function, then I detach the thread so that the
callback function can reach the return point while the second thread
continues, this would in theory allow IUP to continue his main loop while
the worker function did his thing on a second thread. Something like this:

pthread_t thread_id;
int btn_cb(Ihandle *self) {
    pthread_create(&thread_id, NULL, worker_function, NULL);
    int pthread_detach(thread_id);
    return IUP_DEFAULT;
}

The problem is when I run this I get a segfault as soon as the callback
function reach the return point while the second thread is still going. But
before anything else, I want to ask this:
Is my approach with pthread even close to correct for what I am trying to
accomplish? Should I be using threads for something like this?

I’ve also tried with fork and I manage to get the worker function to
execute while having a responsive GUI, but I think would rather avoid
processes and the more complex methods of communication it implies, like
IPC servers or FIFO.

I would really appreciate if anyone can help me with this for my amateur
project.
_______________________________________________
Iup-users mailing list
Iup-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/iup-users

Reply via email to