0

Here the new code snippet:

#define SIMU_TIME 30
pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER ; 
pthread_cond_t condition = PTHREAD_COND_INITIALIZER ; 
void *timer(void *Ptr) 
{    while ((float) clock()/CLOCKS_PRE_TICKS < SIMU_TIME) 
       {  float mean = (float) Ptr ; 
         float interval = exponential(mean) ; 
         float goal = (float) clock()/CLOCKS_PRE_TICKS + interval ; 
         pthread_mutex_lock(&mutex) ;
         while(goal > (float) clock()/CLOCKS_PRE_TICKS ) ; 
        pthread_mutex_unlock(&mymutex) ; 
       }
return(NULL) ; 
}
void *AddPacket(void *Ptr) 
{
   pthread_cond_lock(&mymutex) ;
   pthread_cond_wait(&condition, &mymutex) ; 
     // do business
    pthread_unlock_mutex(&mymutex) ; 
   }
int main()
{  float mean = 1.5 ; 
  pthread_t thread1, thread2 ; 
  pthread_create(&thread1, NULL, &timer, (void *) mean) ; 
  pthread_create(&thread2, NULL, &AddPacket, NULL) ; 
  pthread_join(thread1, NULL) ; 
  pthread_join(thread2, NULL) ; 
  pthread_mutex_destroy(&mymutex) ; 
  pthread_cond_destroy(&condition) ;
  pthread_exit(NULL) ; 
}

Since the pthread_cond_wait is used generally when one thread doesn't access to an attribut until it reaches a certain threshold, we must use a mutex associated with this variable to avoid any race conditions, but in my case, the two threads don't actually need to access to the same memory area, it's just a way to schedule the second thread when the first one allows it. In this situation is it necessary to call "pthread_mutex_lock" before "pthread_cond_wait" ?

Galileo
  • 321
  • 1
  • 4
  • 12

1 Answers1

0

The problem is that you can have two alarms at the same time, or two SIGALRM handlers at the same time. This can be solved in many ways. One way is use event queues.

You also have another problem, and that is that you set the signal handler to this. There are plenty of problems with this. The first is that this is not valid in the functions. The reasons for this is that it's a thread function being called directly by the kernel, and so doesn't have a valid this. The second is that the handler has to be a function, and not a class. Remember that signal handlers are called from the operating system, and it has no concept of C++ classes.

Both the problems above will be solved by implementing some kind of event queue.

There is also a problem with the thread functions, in that when they run to the end the thread ends as well.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Dear mister Joachim, I understand that the fact of having two SIGALARM can lead to important race problems in the code since the systme can't know which function to call for each signal. I tried to make use of "pthread_cond_t" and "pthread_mutex_t" to solve the issue. Please, find above the new code snippet. I would be really thankful if I had you opinion. Thanks – Galileo Aug 06 '12 at 10:21
  • @user16841 In answering another question, I created a simple event queue system. I'm not completely happy with it yet, but it works. See http://stackoverflow.com/a/11866539/440558 – Some programmer dude Aug 08 '12 at 14:27