6 Condition::Condition(int init_value)
8 pthread_mutex_init(&mutex, 0);
9 pthread_cond_init(&cond, NULL);
13 Condition:: ~Condition()
15 pthread_cond_destroy(&cond);
16 pthread_mutex_destroy(&mutex);
20 void Condition::lock()
22 pthread_mutex_lock(&mutex);
23 while(value <= 0) pthread_cond_wait(&cond, &mutex);
25 pthread_mutex_unlock(&mutex);
28 void Condition::unlock()
30 pthread_mutex_lock(&mutex);
32 pthread_cond_signal(&cond);
33 pthread_mutex_unlock(&mutex);
36 int Condition::timed_lock(int milliseconds)
39 struct timespec timeout;
42 pthread_mutex_lock(&mutex);
43 gettimeofday(&now, 0);
44 timeout.tv_sec = now.tv_sec + milliseconds / 1000000;
45 timeout.tv_nsec = now.tv_usec * 1000 + (milliseconds % 1000000) * 1000;
47 while(value <= 0 && result != ETIMEDOUT)
49 result = pthread_cond_timedwait(&cond, &mutex, &timeout);
52 if(result == ETIMEDOUT)
61 pthread_mutex_unlock(&mutex);