42 lines
1.0 KiB
C
42 lines
1.0 KiB
C
/*
|
|
* cond_dynamic.c
|
|
*
|
|
* Demonstrate dynamic initialization of a condition variable.
|
|
*/
|
|
#include <pthread.h>
|
|
#include "errors.h"
|
|
|
|
/*
|
|
* Define a structure, with a mutex and condition variable.
|
|
*/
|
|
typedef struct my_struct_tag {
|
|
pthread_mutex_t mutex; /* Protects access to value */
|
|
pthread_cond_t cond; /* Signals change to value */
|
|
int value; /* Access protected by mutex */
|
|
} my_struct_t;
|
|
|
|
int
|
|
main(int argc, char* argv[])
|
|
{
|
|
my_struct_t* data;
|
|
int status;
|
|
|
|
data = malloc(sizeof(my_struct_t));
|
|
if (data == NULL)
|
|
errno_abort("Allocate structure");
|
|
status = pthread_mutex_init(&data->mutex, NULL);
|
|
if (status != 0)
|
|
err_abort(status, "Init mutex");
|
|
status = pthread_cond_init(&data->cond, NULL);
|
|
if (status != 0)
|
|
err_abort(status, "Init condition");
|
|
status = pthread_cond_destroy(&data->cond);
|
|
if (status != 0)
|
|
err_abort(status, "Destroy condition");
|
|
status = pthread_mutex_destroy(&data->mutex);
|
|
if (status != 0)
|
|
err_abort(status, "Destroy mutex");
|
|
(void) free(data);
|
|
return status;
|
|
}
|