Re: [Iup-users] How to do concurrency with IUP?

2018-12-28 Thread Daniel G.
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 ()
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.  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(_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


Re: [Iup-users] How to do concurrency with IUP?

2018-12-28 Thread Antonio Scuri
  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.  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(_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


Re: [Iup-users] How to do concurrency with IUP?

2018-12-28 Thread Daniel G.
Thanks, I'll give threads another try then, and concentrate on the pthread
api.

El vie., 28 dic. 2018 a las 5:59, Johann Philippe via Iup-users (<
iup-users@lists.sourceforge.net>) escribió:

> 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 Hellp
> Johann
>
> 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(_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
>
___
Iup-users mailing list
Iup-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/iup-users


Re: [Iup-users] How to do concurrency with IUP?

2018-12-28 Thread Johann Philippe via Iup-users
 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 
 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(_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


Re: [Iup-users] How to do concurrency with IUP?

2018-12-28 Thread John Spikowski
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(_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