kenrus
August 1st, 2007, 04:13 PM
I am looking for the best (or just 'a better') way to have main() wait for all the threads to finish.
Right now, all of the threads have access to a structure that contains a count. Each thread increments this count by 1 when it finishes. main() continually checks this count and when it determines all of the threads have finished, it performs cleanup and exits.
#include <pthread.h>
#include <stdio.h>
typedef struct TagThreadData
{
pthread_mutex_t* pMutex;
int* pcthread;
char id[2];
}TThreadData;
void* mutex_test(void*);
int main(int argc, char** argv)
{
int rv_init = 0;
int rv_destroy = 0;
int rv_thread = 0;
int i = 0;
pthread_mutex_t mutex;
int cthread = 0;
TThreadData ThreadData[] = { {&mutex, &cthread, "A"},
{&mutex, &cthread, "B"},
{&mutex, &cthread, "C"},
{&mutex, &cthread, "D"},
{&mutex, &cthread, "E"},
{&mutex, &cthread, "F"},
{&mutex, &cthread, "G"},
{&mutex, &cthread, "H"},
{&mutex, &cthread, "I"},
{ 0, 0, 0}
};
pthread_t threads[10];
//INITIALIZE MUTEX
rv_init = pthread_mutex_init(&mutex, 0);
printf("\n[MAIN]: Mutex Initialization Status: %i", rv_init);
//LAUNCHING THREADS
for(i = 0; ThreadData[i].pMutex != 0; i++)
{
rv_thread = pthread_create(&threads[i], 0, mutex_test, (void*)(&ThreadData[i]));
}
while(cthread < i)
{
time_t start = 0;
time_t now = 0;
time(&start);
while((start + 1) > now)
time(&now);
pthread_mutex_lock(&mutex);
printf("\n[MAIN]: Waiting on threads to finish [%i]", cthread);
pthread_mutex_unlock(&mutex);
}
//DESTROY MUTEX
rv_destroy = pthread_mutex_destroy(&mutex);
printf("\n[MAIN]: Mutex Destruction Status: %i", rv_destroy);
return 0;
}
void* mutex_test(void* pszThreadId)
{
TThreadData* pThreadData = (TThreadData*) pszThreadId;
char* pszId = pThreadData->id;
pthread_mutex_t* pmutex = pThreadData->pMutex;
unsigned int i = 0;
int rv_lock = 0;
int rv_unlock = 0;
time_t start_t = 0;
time_t now_t = 0;
printf("\n[%s]: Starting...", pszId);
for(i = 0; i < 100; i++)
{
time(&start_t);
while((start_t + 1) > now_t)
time(&now_t);
printf("\n[%s]: Attempting to lock mutex...", pszId);
rv_lock = pthread_mutex_lock(pmutex);
printf("\n[%s]: mutex locked - rv_lock == %i", pszId, rv_lock);
time(&start_t);
while((start_t + 1) > now_t)
time(&now_t);
printf("\n[%s]: unlocking the mutex now", pszId);
rv_unlock = pthread_mutex_unlock(pmutex);
printf("\n[%s]: mutex has been unlocked - rv_unlock == %i", pszId, rv_unlock);
}
*(pThreadData->pcthread) += 1;
printf("\n[%s]: Finished - [%i]", pszId, *pThreadData->pcthread);
}
Is there a better way ?
Thanks for any help.
Right now, all of the threads have access to a structure that contains a count. Each thread increments this count by 1 when it finishes. main() continually checks this count and when it determines all of the threads have finished, it performs cleanup and exits.
#include <pthread.h>
#include <stdio.h>
typedef struct TagThreadData
{
pthread_mutex_t* pMutex;
int* pcthread;
char id[2];
}TThreadData;
void* mutex_test(void*);
int main(int argc, char** argv)
{
int rv_init = 0;
int rv_destroy = 0;
int rv_thread = 0;
int i = 0;
pthread_mutex_t mutex;
int cthread = 0;
TThreadData ThreadData[] = { {&mutex, &cthread, "A"},
{&mutex, &cthread, "B"},
{&mutex, &cthread, "C"},
{&mutex, &cthread, "D"},
{&mutex, &cthread, "E"},
{&mutex, &cthread, "F"},
{&mutex, &cthread, "G"},
{&mutex, &cthread, "H"},
{&mutex, &cthread, "I"},
{ 0, 0, 0}
};
pthread_t threads[10];
//INITIALIZE MUTEX
rv_init = pthread_mutex_init(&mutex, 0);
printf("\n[MAIN]: Mutex Initialization Status: %i", rv_init);
//LAUNCHING THREADS
for(i = 0; ThreadData[i].pMutex != 0; i++)
{
rv_thread = pthread_create(&threads[i], 0, mutex_test, (void*)(&ThreadData[i]));
}
while(cthread < i)
{
time_t start = 0;
time_t now = 0;
time(&start);
while((start + 1) > now)
time(&now);
pthread_mutex_lock(&mutex);
printf("\n[MAIN]: Waiting on threads to finish [%i]", cthread);
pthread_mutex_unlock(&mutex);
}
//DESTROY MUTEX
rv_destroy = pthread_mutex_destroy(&mutex);
printf("\n[MAIN]: Mutex Destruction Status: %i", rv_destroy);
return 0;
}
void* mutex_test(void* pszThreadId)
{
TThreadData* pThreadData = (TThreadData*) pszThreadId;
char* pszId = pThreadData->id;
pthread_mutex_t* pmutex = pThreadData->pMutex;
unsigned int i = 0;
int rv_lock = 0;
int rv_unlock = 0;
time_t start_t = 0;
time_t now_t = 0;
printf("\n[%s]: Starting...", pszId);
for(i = 0; i < 100; i++)
{
time(&start_t);
while((start_t + 1) > now_t)
time(&now_t);
printf("\n[%s]: Attempting to lock mutex...", pszId);
rv_lock = pthread_mutex_lock(pmutex);
printf("\n[%s]: mutex locked - rv_lock == %i", pszId, rv_lock);
time(&start_t);
while((start_t + 1) > now_t)
time(&now_t);
printf("\n[%s]: unlocking the mutex now", pszId);
rv_unlock = pthread_mutex_unlock(pmutex);
printf("\n[%s]: mutex has been unlocked - rv_unlock == %i", pszId, rv_unlock);
}
*(pThreadData->pcthread) += 1;
printf("\n[%s]: Finished - [%i]", pszId, *pThreadData->pcthread);
}
Is there a better way ?
Thanks for any help.