forked from hailinzeng/Programming-POSIX-Threads
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cond_dynamic.c
40 lines (37 loc) · 1.11 KB
/
cond_dynamic.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
* 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;
}