Re: glib: processing events in multiple threads

On Tue, Apr 30, 2013 at 12:01 PM, Patrick Ohly <patrick ohly intel com> wrote:
On Tue, 2013-04-30 at 10:44 +0100, jcupitt gmail com wrote:
> You can do this very simple and reliably. For example:
> worker()
> {
>   char *str;
>   for(;;) {
>     str = g_strdup("hello world!\n");
>     g_idle_add(from_worker_cb, str);
>     sleep(1);
>   }
> }
> gboolean
> from_worker(char *str)
> {
>   update_textview(str);
>   g_free(str);
>   return FALSE;
> }

This is one-way. How about two-way communication between threads?

asynchronous queues are your friend here...  one of two ways:
  • have a thread that reads the async queue, followed by the appropriate call depending on the message read, where all other threads write to the Q.


  • two async queues, one in each thread, where each is a read/write of the other

this would depend on the how much you need here.  between only two threads you could get away with the 2nd option.  more than that, and you should go with the first.

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