-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread_counter.c
53 lines (44 loc) · 1.31 KB
/
thread_counter.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
41
42
43
44
45
46
47
48
49
50
51
52
#include "thread_counter.h"
void thread_counter_init(thread_counter_t *thread_counter)
{
thread_counter->count = 0;
pthread_mutex_init(&thread_counter->lock, NULL);
}
void thread_counter_destroy(thread_counter_t *thread_counter)
{
pthread_mutex_destroy(&thread_counter->lock);
}
void thread_counter_increase(thread_counter_t *thread_counter)
{
pthread_mutex_lock(&thread_counter->lock);
thread_counter->count++;
pthread_mutex_unlock(&thread_counter->lock);
}
void thread_counter_decrease(thread_counter_t *thread_counter)
{
pthread_mutex_lock(&thread_counter->lock);
thread_counter->count--;
pthread_mutex_unlock(&thread_counter->lock);
}
int thread_counter_get(thread_counter_t *thread_counter)
{
int c;
pthread_mutex_lock(&thread_counter->lock);
c = thread_counter->count;
pthread_mutex_unlock(&thread_counter->lock);
return c;
}
void thread_counter_set(thread_counter_t *thread_counter, int count)
{
pthread_mutex_lock(&thread_counter->lock);
thread_counter->count = count;
pthread_mutex_unlock(&thread_counter->lock);
}
int thread_counter_compare(thread_counter_t *thread_counter, int count)
{
int r;
pthread_mutex_lock(&thread_counter->lock);
r = thread_counter->count - count;
pthread_mutex_unlock(&thread_counter->lock);
return r;
}