forked from 834810071/NetworkProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcondition.cpp
More file actions
79 lines (70 loc) · 1.51 KB
/
condition.cpp
File metadata and controls
79 lines (70 loc) · 1.51 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//
// Created by jxq on 19-8-15.
//
#include "condition.h"
#include <pthread.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int condition_init(condition_t *cond)
{
int state;
state = pthread_cond_init(&(cond->pcond), NULL);
if (state == 0)
{
return state;
}
state = pthread_mutex_init(&(cond->pmutex), NULL);
if (state == 0)
{
return state;
}
return 0;
}
int condition_lock(condition_t *cond)
{
//cout << pthread_mutex_lock(&(cond->pmutex)) << endl;
if (pthread_mutex_lock(&(cond->pmutex)) == 0)
{
printf("pthread_mutex_lock error\n");
exit(EXIT_FAILURE);
}
return 1;
}
int condition_unlock(condition_t *cond)
{
pthread_mutex_unlock(&(cond->pmutex));
}
int condition_wait(condition_t *cond)
{
return pthread_cond_wait(&(cond->pcond), &(cond->pmutex));
}
int condition_timewait(condition_t *cond, const struct timespec *abstime)
{
return pthread_cond_timedwait(&(cond->pcond), &(cond->pmutex), abstime);
}
int condition_signal(condition_t *cond)
{
return pthread_cond_signal(&(cond->pcond));
}
int condition_broadcast(condition_t *cond)
{
return pthread_cond_broadcast(&(cond->pcond));
}
int condition_destroy(condition_t *cond)
{
int state;
state = pthread_cond_destroy(&(cond->pcond));
if (state == 0)
{
return state;
}
state = pthread_mutex_destroy(&(cond->pmutex));
if (state == 0)
{
return state;
}
return 0;
}