Re: A question about threads



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Mon, Dec 11, 2006 at 11:13:31AM +0100, Paolo Franzetti wrote:
Hi.

I would like to put in a GTK application a button which is
"connected" to a function that performs a very long computational
task which do not freezes the application.
[...]
I guess I should use "threads", and I have tried to do in many different
ways but my application always freezes.

This is not the only option. Basically, I see three main options
(ordered from tight to loose coupling):

(1) Run the computation in an idle handler.
    For this you'd have to break up your computation in little chunks
    ("steps") which get called from time to time when the app has
    "nothing to do". Basically, you set up the data needed by your
    computation in a struct, allocate and initialize it and
    gtk_idle_add() your "step function".

    * Pros: IMHO the most simple and robust variant. Quite easy to
      update a progress bar from the step function and so on. Tight
      control of the running process.
    * Cons: you have to munge your long running process to be executed
      in chunks (which have to return fairly quickly: otherwise the GUI
      seems to "freeze").

(2) Run in a separate thread. I have very little experience with
    threads, so I won't say much here. AFAIK you have to keep all your
    widget code in one thread, whatever that means.

(3) Run in a separate process, communicate with pipes/shared mem what
    not.
    * Pros: separate address space, which adds quite a bit of robustness.
      If you go the socket way, you might run the "long computation on
      another machine!
    * Cons: picking/developing a client/server protocol is less-than
      trivial (but gobs of fun :)

I usually go with (1).

There is some flowing between those alternatives (e.g. if you implement
(3), you might regard the protocol handler, which would typically sit on
an idle or input function as "the long running app" of cases (1) or (2).

- -- tomÃs
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFFfVlBBcgs9XrR2kYRAn6xAJsHK9n4FT3NsaERGIepkXKoMteZ8QCdE/CO
6nru1dwe9IxFDvMw8zpsIkQ=
=AeIv
-----END PGP SIGNATURE-----




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