bug-commoncpp
[Top][All Lists]
Advanced

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

Timeout and threads


From: Daniel Wagner
Subject: Timeout and threads
Date: Mon, 18 Aug 2003 17:09:40 +0200
User-agent: Gnus/5.1003 (Gnus v5.10.3) Emacs/21.2 (gnu/linux)

Hi,

I'm using the GNU Common C++ framework for my project.  So far I very
pleased with it.  The only thing I couldn't figure out yet is
following.

I have a main object which creates some plugin objects which run as
thread.  There are two ways in which the program should terminated.
The first is that all plugins terminated normally (all work is done)
or a timeout is reached.  For the second case the main object should
terminated the plugin objects.

My first implementation in C has used following code for this job:


...
  n = io_plugins_instances;
  while (n)
    {
      p = (io_plugin_t *)n->data;
      io_plugin_start (p);
      n = n->next;
    }
  
  rc = pthread_create (&collector_thread, NULL, 
                       (void *)plugin_thread_collector,
                       (void *)NULL);
  if (rc)
    {
      fprintf(stderr, "return code from pthread_create() is %d\n", rc);
      exit (-1); 
    }

  pthread_mutex_lock (&mut);
  gettimeofday (&now, NULL);
  timeout.tv_sec = now.tv_sec + options->timeout / 1000;
  timeout.tv_nsec = now.tv_usec + (options->timeout % 1000) * 1000;
  
  rc = pthread_cond_timedwait (&cond, &mut, &timeout);
  if (rc == ETIMEDOUT) 
...


static void *
plugin_thread_collector (void)
{
  GList *n;
  io_plugin_t *p;
  int rc, status;

  n = io_plugins_instances;
  while (n)
    {
      p = (io_plugin_t *)n->data;
      
      if (p->xmit_iso_thread)
        {
          rc = pthread_join(p->xmit_iso_thread, (void **)&status);
          if (rc)
            {
              fprintf(stderr, "return code from pthread_join() is %d\n", rc);
              exit(-1);
            }
          DBGMSG ("plugin_thread_collector", "Completed join with thread %p 
status= %d", &p->xmit_iso_thread, status);
        }
...

The question is how I could model the same with GNU Common C++?  I saw
there is a class called Event?  Should I try it to solve with that
one?


thanks,
daniel

ps: I'm not on the list, so please Cc me.  thanks.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]