Re: Another thread/idle/timeout question



On 5 July 2012 14:26, David Buchan <pdbuchan yahoo com> wrote:
Is there a way to have a (non-main iteration) thread issue a signal when it ends?

I start up a timeout and an idle function when I spawn a new thread.
I want the main iteration to stop the timeout and idle function as soon as the new thread is finished and 
disappears.

You can use g_idle_add() to send a message from the worker to the main thread.

The worker might end like this:

worker_thread( .. )
{
  ..
  final_bit_of_work();
  g_idle_add( thread_done_cb, myself);
  return NULL;
}

and the main thread cleans up and stops like this:

gboolean
thread_done_cb(Worker *worker)
{
  // unlink the main thread from the worker
  g_source_remove(worker->timeout_sid);

  // wait for the worker to finish
  g_thread_join(worker->gthread);

  // all done!
  worker_free(worker);

  return FALSE;
}

Or perhaps you mean something more complicated and I've misunderstood
:( I use semaphores for hairier thread synchronisation problems.

http://en.wikipedia.org/wiki/Semaphore_%28programming%29

I have a set of simple semaphore functions over the g_thread primitives:

https://github.com/jcupitt/libvips/blob/master/libvips/iofuncs/semaphore.c

John



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