I think using threads is the right solution here. But be careful with those 
points :-it's recommanded not to alloc memory inside the threads routine. It's 
betterave allocating before threads runs. -if sole variables are acessed on 
both threads at the same Time, it may crash. Then you should use mutexes to 
lock data accessing.
Hope it Can HellpJohann 
    Le vendredi 28 décembre 2018 à 10:43:26 UTC+1, John Spikowski 
<supp...@scriptbasic.org> a écrit :  
 
 Hi Dan,
I was able to get IUP to work in a threaded environment under Windows but not 
Linux (Gtk) using Script BASIC. 
John
On Fri, 2018-12-28 at 01:54 -0600, Daniel G. wrote:
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 
listiup-us...@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/iup-users

_______________________________________________
Iup-users mailing list
Iup-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/iup-users
  
_______________________________________________
Iup-users mailing list
Iup-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/iup-users

Reply via email to