-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab_02.cpp
More file actions
200 lines (182 loc) · 5.71 KB
/
lab_02.cpp
File metadata and controls
200 lines (182 loc) · 5.71 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <pthread.h>
#include <unistd.h>
#include <time.h>
#include <iostream>
#include <string.h>
#include "getch.cpp"
using namespace std;
void* some_p1(void * param);
void* some_p2(void * param);
// #define LOCK
// #define TRY_LOCK
#define TIMED_LOCK
// #define LOCK_MUTEX_ON_START
struct p_args
{
bool work;
pthread_mutex_t* mutex;
};
int main(int argc, char* argv[])
{
pthread_t my_thread_1, my_thread_2;
pthread_attr_t my_attr_1;
pthread_attr_init(&my_attr_1);
pthread_mutex_t mutex;
pthread_mutex_init(&mutex, nullptr);
#ifdef LOCK_MUTEX_ON_START
pthread_mutex_lock(&mutex);
#endif // LOCK_MUTEX_ON_START
p_args p1_arg = {true, &mutex};
p_args p2_arg = {true, &mutex};
pthread_create(&my_thread_1, &my_attr_1, some_p1, (void*)&p1_arg);
pthread_create(&my_thread_2, &my_attr_1, some_p2, (void*)&p2_arg);
getch();
p1_arg.work = false;
p2_arg.work = false;
pthread_join(my_thread_1, nullptr);
pthread_join(my_thread_2, nullptr);
cout << endl;
return 0;
}
void* some_p1(void* param)
{
p_args* args = (p_args*)param;
timespec tr = {0, 1000*1000*500};
timespec locktime = {2, 0};
int my_error = 0;
while(args->work)
{
#ifdef LOCK
if(pthread_mutex_lock(args->mutex) == 0)
{
for(size_t i = 0; i < 5; i++)
{
write(1, "1", sizeof("1"));
nanosleep(&tr, nullptr);
}
#ifdef TEST_MUTEX_LOCK
pthread_exit(0);
#endif
}
pthread_mutex_unlock(args->mutex);
sleep(1);
#endif // LOCK
#ifdef TRY_LOCK
if((my_error = pthread_mutex_trylock(args->mutex)) == 0)
{
for(size_t i = 0; i < 5; i++)
{
write(1, "1", sizeof("1"));
// nanosleep(&tr, nullptr);
}
write(1, "\n", sizeof("\n"));
#ifdef TEST_MUTEX_LOCK
pthread_exit(0);
#endif
sleep(2);
pthread_mutex_unlock(args->mutex);
}
else
{
// sleep(1);
// write(1, "\n[Поток 1]: mutex занят\n", sizeof("\n[Поток 1]: mutex занят\n"));
write(1, "[Поток 1]: ", sizeof("[Поток 1]: "));
// perror("[Поток 1]");
write(1, strerror(my_error), strlen(strerror(my_error)));
write(1, "\n", strlen("\n"));
// sleep(1);
// strerror(my_error);
}
sleep(1);
#endif // TRY_LOCK
#ifdef TIMED_LOCK
clock_gettime(CLOCK_REALTIME, &locktime);
locktime.tv_sec += 5;
if(pthread_mutex_timedlock(args->mutex, &locktime) == 0)
{
for(size_t i = 0; i < 5; i++)
{
write(1, "1", sizeof("1"));
// nanosleep(&tr, nullptr);
}
pthread_mutex_unlock(args->mutex);
#ifdef TEST_MUTEX_LOCK
pthread_exit(0);
#endif
}
else continue;
sleep(1);
#endif // TIMED_LOCK
}
pthread_exit(0);
}
void* some_p2(void* param)
{
p_args* args = (p_args*)param;
timespec tr = {0, 1000*1000*500};
timespec locktime = {2, 0};
int my_error = 0;
while(args->work)
{
#ifdef LOCK
if(pthread_mutex_lock(args->mutex) == 0)
{
for(size_t i = 0; i < 5; i++)
{
write(1, "2", sizeof("2"));
nanosleep(&tr, nullptr);
}
#ifdef TEST_MUTEX_LOCK
pthread_exit(0);
#endif
}
pthread_mutex_unlock(args->mutex);
sleep(1);
#endif // LOCK
#ifdef TRY_LOCK
if((my_error = pthread_mutex_trylock(args->mutex)) == 0)
{
for(size_t i = 0; i < 5; i++)
{
write(1, "2", sizeof("2"));
// nanosleep(&tr, nullptr);
}
write(1, "\n", sizeof("\n"));
#ifdef TEST_MUTEX_LOCK
pthread_exit(0);
#endif
sleep(2);
pthread_mutex_unlock(args->mutex);
}
else
{
// write(1, "\n[Поток 2]: mutex занят\n", sizeof("\n[Поток 2]: mutex занят\n"));
write(1, "[Поток 2]: ", sizeof("[Поток 2]: "));
// perror("[Поток 2]");
write(1, strerror(my_error), strlen(strerror(my_error)));
write(1, "\n", strlen("\n"));
// sleep(1);
}
sleep(1);
#endif // TRY_LOCK
#ifdef TIMED_LOCK
clock_gettime(CLOCK_REALTIME, &locktime);
locktime.tv_sec += 5;
if(pthread_mutex_timedlock(args->mutex, &locktime) == 0)
{
for(size_t i = 0; i < 5; i++)
{
write(1, "2", sizeof("2"));
// nanosleep(&tr, nullptr);
}
pthread_mutex_unlock(args->mutex);
#ifdef TEST_MUTEX_LOCK
pthread_exit(0);
#endif
}
else continue;
sleep(1);
#endif // TIMED_LOCK
}
pthread_exit(0);
}