-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpthread_conditon.cpp
More file actions
129 lines (118 loc) · 3.05 KB
/
pthread_conditon.cpp
File metadata and controls
129 lines (118 loc) · 3.05 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
* ==================================================
* @file pthread_conditon.cpp
* @brief 条件变量--实现一个线程安全的队列
* @author ywj
* @date 2025-06-22 17:29
* @version 1.0
* @copyright Copyright (c) 2025 ywj. All Rights Reserved.
* ==================================================
*/
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
#include<stdio.h>
#include<queue>
//注意要先通知后释放锁
struct SafeQueue
{
void init(size_t capacity)
{
pthread_mutex_lock(&_mutex);
_capacity = capacity;
pthread_mutex_unlock(&_mutex);
}
void push(int val)
{
//获取锁
pthread_mutex_lock(&_mutex);
//判断是否非满,可能存在虚假唤醒所有用循环
while (_size >= _capacity)
{
//等待非满
pthread_cond_wait(&_condNotFull, &_mutex);
}
//被条件非满条件变量唤醒
_queue.push(val);
++_size;
//通知非空
pthread_cond_signal(&_condNotEmpty);
//释放锁
pthread_mutex_unlock(&_mutex);
}
int pop()
{
//获取锁
pthread_mutex_lock(&_mutex);
while (_size == 0)
{
//等待非空条件变量通知
pthread_cond_wait(&_condNotEmpty, &_mutex);
}
//唤醒之后
int ret = _queue.front();
_queue.pop();
--_size;
//通知非满
pthread_cond_signal(&_condNotFull);
//释放锁
pthread_mutex_unlock(&_mutex);
return ret;
}
~SafeQueue()
{
pthread_mutex_destroy(&_mutex);
pthread_cond_destroy(&_condNotEmpty);
pthread_cond_destroy(&_condNotFull);
}
std::queue<int> _queue;
size_t _size = 0;
size_t _capacity = 100;
pthread_mutex_t _mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t _condNotEmpty = PTHREAD_COND_INITIALIZER;
pthread_cond_t _condNotFull = PTHREAD_COND_INITIALIZER;
};
SafeQueue safeQueue;
void* threadFunc(void* arg)
{
for (int i = 0; i < 100; i++)
{
safeQueue.push(i);
printf("thread %lu push safeQueue-->%d\n", pthread_self(), i);
}
return NULL;
}
void* threadFunc1(void* arg)
{
for (int i = 100; i < 200; i++)
{
safeQueue.push(i);
printf("thread %lu push safeQueue-->%d\n", pthread_self(), i);
}
return NULL;
}
void* threadFunc2(void* arg)
{
for (int i = 0; i < 200; i++)
{
int val = safeQueue.pop();
printf("thread %lu pop safeQueue-->%d\n", pthread_self(), val);
}
return NULL;
}
int main()
{
//初始化
safeQueue.init(100);
pthread_t pushThread1, pushThread2, popThread;
if (pthread_create(&pushThread1, NULL, threadFunc, NULL) || pthread_create(&pushThread2,NULL, threadFunc1,NULL) ||
pthread_create(&popThread, NULL, threadFunc2,NULL))
{
printf("create pthread fail\n");
exit(1);
}
pthread_join(pushThread1,NULL);
pthread_join(pushThread2,NULL);
pthread_join(popThread,NULL);
return 0;
}