Re: Concurrent processes



On Tue, Apr 03, 2001 at 11:31:44PM +0200, Valery Avaux wrote:
> Hello,
> 
> I'm trying to build up a physics simulation with Glade and GTK. The main
> function of the simulation is a function called isingflip(). This
> function must do interations up to 1000000 times but the calculation is
> based on two variables controlled by two scrollbars defined in the main
> window. I have already put signal to pass the value of the scrollbars to
> the variables and it works. But when I launch the simulation by clicking
> on a button called "Simulation", the program does the calculations but
> while doing this, I cannot move anymore the scrollbars to change
> parameters.
> 
> Is there a way to let me change parameters ? Maybe lookup signals or
> somenthing like that ? I've heard something about threads but I don't
> know what it is.... I'm really a newbie so if someone could give me some
> very detailled way to do this, it would be very nice.

The reason you can't move the scrollbars is because your program is iterating in isingflip(), and it can only do one thing at once. Paying attention to what the user is doing with the scrollbars counts as another thing.

Two ways to solve this. The first way is to put some code in the middle of isingflip(), which directs the program back into gtk so it can deal with whatever the user has been doing to the scrollbars. that code usually looks something like this, I think:

while (gtk_main_pending())
    gtk_main_iterate();

while gtk has stuff to do (events pending), process an event. Put this code in the loop so it gets run every iteration or something.

This might or might not be convenient and easy to do, depending on what isingflip() looks like. If it's not, there is..

Solution #2: make the program threaded. A "threaded program" is basically a program with built in multitasking, so it can do several things at once. The program is divided into little sub processes (threads), each with their own section of program to compute, at the same time. In your case you would let the main program concentrate on isingflipping, and you would make a separate thread to concentrate on gtk. The pthreads library is used for this.

But be warned, once you play with threads you are suddenly faced with a LOT of new traps and pitfalls and generally nasty things that you have to keep watch for in all the code you write, like maintaining reentrancy and thread safety, and I for one am terrified of threads for that reason :).


> 
> Thanks a lot
> 
> Valery Avaux
> 
> _______________________________________________
> gtk-list mailing list
> gtk-list gnome org
> http://mail.gnome.org/mailman/listinfo/gtk-list




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]