/***************************************************
*--------------------------------------------------
* Creat : 2001. 04. 10 (programed by Cori-Young )
* Site: http://www.byoneself.co.kr
* Update :
*--------------------------------------------------
* Compile : cc -o xxx xxx.c -lpthread -lrt
*--------------------------------------------------
* Machine hardware: sun4u
* OS version: 5.7
* Processor type: sparc
* Hardware: SUNW,Ultra-60
***************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#define _MULTI_THREADED
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex2;
pthread_mutex_t mutex3;
static void checkResults(char *string, int rc) {
if (rc) {
printf("Error on : %s, rc=%d", string, rc);
exit(EXIT_FAILURE);
}
return;
}
int main(int argc, char **argv)
{
int rc=0;
pthread_mutexattr_t mta;
printf("Enter Testcase - %sn", argv[0]);
printf("Create a default mutex attributen");
rc = pthread_mutexattr_init(&mta);
checkResults("pthread_mutexattr_initn", rc);
printf("Create the mutexes using the default mutex attributesn");
printf("First mutex created via static PTHREAD_MUTEX_INITIALIZERn");
printf("Create the mutex using the NULL attributes (default)n");
rc = pthread_mutex_init(&mutex3, NULL);
checkResults("pthread_mutex_init(NULL)n", rc);
printf("Create the mutex using a mutex attributes objectn");
rc = pthread_mutex_init(&mutex2, &mta);
checkResults("pthread_mutex_init(mta)n", rc);
printf("- At this point, all mutexes can be used with theirn");
printf("- default attributes from any threads that want ton");
printf("- use themn");
printf("Destroy all mutexesn");
pthread_mutex_destroy(&mutex);
pthread_mutex_destroy(&mutex2);
pthread_mutex_destroy(&mutex3);
printf("Main completedn");
return 0;
} |
|