I join to this email 2 pieces of code : a test using pthread, an other one using APR. Both work fine without valgrind. Using the profiling tool with the apr test, the assertion failed because of the wait condition.
Tangui
#include <pthread.h> #include <stdio.h> #include <assert.h>
int cpt; int pointers[2]; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond = PTHREAD_COND_INITIALIZER; /** * Function executed on a different thread */ void* fonct(void* f) { pthread_mutex_lock(&mutex); //cpt must be 0 assert(pointers[cpt] != 0); //Signal to the main thread we read the good cpt value pthread_cond_signal(&cond); printf("Signaled\n"); pthread_mutex_unlock(&mutex); } int main(int argv, char* argc) { // Creation and initialization pthread_t thread; cpt = 0; pointers[0] = 2; pointers[1] = 0; pthread_mutex_lock(&mutex); pthread_create(&thread, NULL, fonct, NULL); //here we wait until the other thread send us a signal pthread_cond_wait(&cond, &mutex); //Change the cpt value to see if the other thread get confused cpt++; printf("Wait done\n"); pthread_mutex_unlock(&mutex); sleep(2); /** * We should here desctruct everything */ }
#include <apr_thread_cond.h> #include <apr_thread_proc.h> #include <apr_thread_mutex.h> #include <apr_pools.h> #include <stdio.h> #include <assert.h> int cpt; int pointers[2]; apr_thread_mutex_t * mutex; apr_thread_cond_t * cond; /** * Function executed on a different thread */ void* APR_THREAD_FUNC fonct(apr_thread_t* t, void* f) { apr_thread_mutex_lock(mutex); //cpt must be 0 assert(pointers[cpt] != 0); //Signal to the main thread we read the good cpt value apr_thread_cond_signal(cond); printf("Signaled\n"); apr_thread_mutex_unlock(mutex); } int main(int argv, char* argc) { // Creation and initialization apr_thread_t * thread; apr_pool_t * pool; apr_pool_initialize(); apr_pool_create(&pool, NULL); cpt = 0; pointers[0] = 2; pointers[1] = 0; apr_thread_mutex_create (&mutex, APR_THREAD_MUTEX_DEFAULT, pool); apr_thread_cond_create(&cond, pool); apr_thread_mutex_lock(mutex); apr_thread_create(&thread, NULL, fonct, NULL, pool); //here we wait until the other thread send us a signal apr_thread_cond_wait(cond, mutex); //Change the cpt value to see if the other thread get confused cpt++; printf("Wait done\n"); apr_thread_mutex_unlock(mutex); sleep(2); /** * We should here desctruct everything */ }
APR=`apr-config --link-ld --includes` All: apr_test pthread_test apr_test: testAPRCondition.c $(CC) $(APR) -o apr_test testAPRCondition.c pthread_test: testPThreadCondition.c $(CC) -pthread -o pthread_test testPThreadCondition.c
begin:vcard fn:Tangui Morlier n:Morlier;Tangui org:INRIA Futurs;Grand Large Project email;internet:[EMAIL PROTECTED] title:Associate Engineer tel;work:+33 (0)1 69 15 42 32 url:http://www.lri.fr/~tmorlier/?signature version:2.1 end:vcard