Thanks Antonio, I already tried with IupLoopStep, but one of the functions
I use take too long to finish to keep the GUI responsive. But I'm happy to
say that i found the problem with program, at the same time it's kind of
shameful for me, but just for future reference:

It turns out I was creating a struct in the callback function, and giving
that struct to the thread, I was smart enough to comment the free() where I
free the structure, but totally forgot that the struct would still lose
context and give me a segfault as expected.

Here’s a quick dirty example for future reference:
https://pastebin.com/6bZdrRYA

There was nothing wrong with how I was using the pthread api or the IUP
api. It took me running the program line by line with GDB to finally
realize this. Such a noob mistake to make, but I guess it’s bound to happen
when learning all this self-taught.

El vie., 28 dic. 2018 a las 12:12, Antonio Scuri (<antonio.sc...@gmail.com>)
escribió:

>   Hi,
>
>   Sorry I don't have any example with threads to send you.
>
>   If your long processing is open, I mean, if you can insert a function
> call inside the processing loop, then you can call IupLoopStep from inside
> the loop to turn the user interface responsive while processing. In this
> case no threads will be necessary.
>
> Best,
> Scuri
>
>
> Em sex, 28 de dez de 2018 05:55, Daniel G. <danigaritaro...@gmail.com
> escreveu:
>
>> 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
>>
> _______________________________________________
> 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