Hye,
I am new to pThreads. I am using pThreads Programmin by Bradford
Nichols from O'Reilly.
The code for an example demostrating Conditional veriables is as
follows:
#include <stdio.h>
#include <pthread.h>
#define TCOUNT 10
#define WATCH_COUNT 12
int count = 0;
pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t count_threshold_cv = PTHREAD_COND_INITIALIZER;
int thread_ids[3] = {0, 1, 2};
void watch_count(int *idp){
pthread_mutex_lock(&count_mutex);
while (count <= WATCH_COUNT){
pthread_cond_wait(&count_threshold_cv, &count_mutex);
printf("\n watch_count(): Thread %d, Count is %d \n", *idp,
count);
}
pthread_mutex_unlock(&count_mutex);
}
void inc_count(int *idp){
int i;
for (i=0;i<TCOUNT; i++){
pthread_mutex_lock(&count_mutex);
count++;
printf("\n inc_counter(): Thread %d, old count %d New Count
%d \n", *idp, count-1, count);
if (count == WATCH_COUNT)
pthread_cond_signal(&count_threshold_cv);
pthread_mutex_unlock(&count_mutex);
}
}
main(){
pthread_t threads[3];
pthread_create(&threads[0], NULL, (void *(*)(void *))inc_count,
&thread_ids[0]);
pthread_create(&threads[1], NULL, (void *(*)(void *))inc_count,
&thread_ids[1]);
pthread_create(&threads[2], NULL, (void *(*)(void *))watch_count,
&thread_ids[2]);
pthread_join(threads[0], NULL);
pthread_join(threads[1], NULL);
pthread_join(threads[2], NULL);
}
I expected the o/p to contain
watch_count(): Thread 2 count:12
But, it did not. I traced the prog. and found out, by the time the
watch thread checks count - its already beyond 12. So I modified the
code such that the Watch thread - Thread 3 is created first. This
modified the o/p with the watch thread printing :
watch_count(): Thread 2 count: 20.
I am thinking that the watch thread is not able to lock the mutex till
the two threads are done. If it is, what is the use of the conditional
variable and the pthread_cond_signal().
o/p:
inc_counter(): Thread 0, old count 0 New Count 1
inc_counter(): Thread 0, old count 1 New Count 2
inc_counter(): Thread 0, old count 2 New Count 3
inc_counter(): Thread 0, old count 3 New Count 4
inc_counter(): Thread 0, old count 4 New Count 5
inc_counter(): Thread 0, old count 5 New Count 6
inc_counter(): Thread 0, old count 6 New Count 7
inc_counter(): Thread 0, old count 7 New Count 8
inc_counter(): Thread 0, old count 8 New Count 9
inc_counter(): Thread 0, old count 9 New Count 10
inc_counter(): Thread 1, old count 10 New Count 11
inc_counter(): Thread 1, old count 11 New Count 12
inc_counter(): Thread 1, old count 12 New Count 13
inc_counter(): Thread 1, old count 13 New Count 14
inc_counter(): Thread 1, old count 14 New Count 15
inc_counter(): Thread 1, old count 15 New Count 16
inc_counter(): Thread 1, old count 16 New Count 17
inc_counter(): Thread 1, old count 17 New Count 18
inc_counter(): Thread 1, old count 18 New Count 19
inc_counter(): Thread 1, old count 19 New Count 20
watch_count(): Thread 2, Count is 20
I am using Solaris 7.
Any suggestions would be very welcome.
regards,
=========
Chadalavada Kalyana Krishna,
National PARAM Super Computing Facility,
Center for Development of Advanced Computing,
Pune University Campus,
Pune - 411007
India.
Fax : +91-20-5694081.