kenrus
July 31st, 2007, 07:41 PM
Platform: AIX
I am new to using mutex'es and pthreads so I have been writing some very simple test programs to just understand the concept and the syntax.
Well, my first non-threaded app seemed to work just fine with mutex'es (the function calls did not return any errors), but now that I have introduced pthreads and some of the mutex functions are now returning errors.
Here is the code:
#include <pthread.h>
#include <stdio.h>
//GLOBAL MUTEX
pthread_mutex_t gMutex;
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_t threads[10];
char thread_ids[10][2] ={"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"};
//INITIALIZE MUTEX
rv_init = pthread_mutex_init(&gMutex, 0);
printf("\nMutex Initialization Status: %i", rv_init);
//LAUNCHING THREADS
for(i = 0; i < 2; i++)
{
rv_thread = pthread_create(&threads[i], 0, mutex_test, (void*)(&thread_ids[i]));
}
//DESTROY MUTEX
rv_destroy = pthread_mutex_destroy(&gMutex);
printf("\nMutex Destruction Status: %i", rv_destroy);
pthread_exit(0);
return 0;
}
void* mutex_test(void* pszThreadId)
{
char* pszId = (char*)pszThreadId;
unsigned int i = 0;
int rv_lock = 0;
int rv_unlock = 0;
time_t start_t = 0;
time_t now_t = 0;
for(i = 0; i < 4; i++)
{
printf("\n[%s]: Starting...", pszId);
printf("\n[%s]: Attempting to lock mutex...", pszId);
rv_lock = pthread_mutex_lock(&gMutex);
printf("\n[%s]: mutex locked - rv_lock == %i", pszId, rv_lock);
printf("\n[%s]: holding for a sec ...", pszId);
time(&start_t);
while((start_t + 3) > now_t)
time(&now_t);
printf("\n[%s]: unlocking the mutex now", pszId);
rv_unlock = pthread_mutex_unlock(&gMutex);
printf("\n[%s]: mutex has been unlocked - rv_unlock == %i", pszId, rv_unlock);
}
}
The functions in main both return 0 - success:
pthread_mutex_init()
pthread_mutex_destroy()
The function in the thread (mutex_test) both return 22
pthread_mutex_lock()
pthread_mutex_unlock()
My first, non-threaded, attempt just had all the code in main(), now I have moved the mutex (gMutex) to being a global variable and it is being initialized in main, but used in the thread function (mutex_test)
Any help would be appreciated.
Thanks
I am new to using mutex'es and pthreads so I have been writing some very simple test programs to just understand the concept and the syntax.
Well, my first non-threaded app seemed to work just fine with mutex'es (the function calls did not return any errors), but now that I have introduced pthreads and some of the mutex functions are now returning errors.
Here is the code:
#include <pthread.h>
#include <stdio.h>
//GLOBAL MUTEX
pthread_mutex_t gMutex;
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_t threads[10];
char thread_ids[10][2] ={"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"};
//INITIALIZE MUTEX
rv_init = pthread_mutex_init(&gMutex, 0);
printf("\nMutex Initialization Status: %i", rv_init);
//LAUNCHING THREADS
for(i = 0; i < 2; i++)
{
rv_thread = pthread_create(&threads[i], 0, mutex_test, (void*)(&thread_ids[i]));
}
//DESTROY MUTEX
rv_destroy = pthread_mutex_destroy(&gMutex);
printf("\nMutex Destruction Status: %i", rv_destroy);
pthread_exit(0);
return 0;
}
void* mutex_test(void* pszThreadId)
{
char* pszId = (char*)pszThreadId;
unsigned int i = 0;
int rv_lock = 0;
int rv_unlock = 0;
time_t start_t = 0;
time_t now_t = 0;
for(i = 0; i < 4; i++)
{
printf("\n[%s]: Starting...", pszId);
printf("\n[%s]: Attempting to lock mutex...", pszId);
rv_lock = pthread_mutex_lock(&gMutex);
printf("\n[%s]: mutex locked - rv_lock == %i", pszId, rv_lock);
printf("\n[%s]: holding for a sec ...", pszId);
time(&start_t);
while((start_t + 3) > now_t)
time(&now_t);
printf("\n[%s]: unlocking the mutex now", pszId);
rv_unlock = pthread_mutex_unlock(&gMutex);
printf("\n[%s]: mutex has been unlocked - rv_unlock == %i", pszId, rv_unlock);
}
}
The functions in main both return 0 - success:
pthread_mutex_init()
pthread_mutex_destroy()
The function in the thread (mutex_test) both return 22
pthread_mutex_lock()
pthread_mutex_unlock()
My first, non-threaded, attempt just had all the code in main(), now I have moved the mutex (gMutex) to being a global variable and it is being initialized in main, but used in the thread function (mutex_test)
Any help would be appreciated.
Thanks