-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab_01.cpp
More file actions
84 lines (75 loc) · 1.88 KB
/
lab_01.cpp
File metadata and controls
84 lines (75 loc) · 1.88 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
#include <pthread.h>
#include <unistd.h>
#include <iostream>
#include <stdio.h>
#include <time.h>
#include "getch.cpp"
using namespace std;
void* some_p1(void * param);
void* some_p2(void * param);
int main(int argc, char* argv[])
{
pthread_t my_thread_1, my_thread_2;
bool p1_work = true;
bool p2_work = true;
pthread_attr_t my_attr_1;
pthread_attr_init(&my_attr_1);
if(pthread_create(&my_thread_1, &my_attr_1, some_p1, (void*)&p1_work) == 0)
{
cout << "Поток 1 начал работу" << endl;
}
if(pthread_create(&my_thread_2, &my_attr_1, some_p2, (void*)&p2_work) == 0)
{
cout << "Поток 2 начал работу" << endl;
}
getch();
p1_work = false;
p2_work = false;
void* status_1, *status_2;
pthread_join(my_thread_1, &status_1);
pthread_join(my_thread_2, &status_2);
cout << endl;
cout << "Поток-1 вернул: " << *(int*)status_1 << endl;
cout << "Поток-2 вернул: " << *(int*)status_2 << endl;
free((int*)status_1);
free((int*)status_2);
return 0;
}
void* some_p1(void* param)
{
sleep(1);
int cnt = 0;
timespec tr = {0, 1000*1000*250};
while(*(bool*)param)
{
cout << "1";
cout.flush();
// printf("1");
// write(1, "1", sizeof("1"));
cnt++;
// sleep(1);
nanosleep(&tr, nullptr);
}
int* ret_status = new int;
*ret_status = cnt;
pthread_exit((void*)ret_status);
}
void* some_p2(void* param)
{
sleep(1);
int cnt = 0;
timespec tr = {0, 1000*1000*500};
while(*(bool*)param)
{
cout << "2";
cout.flush();
// printf("2");
// write(1, "2", sizeof("2"));
cnt++;
// sleep(1);
nanosleep(&tr, nullptr);
}
int* ret_status = new int;
*ret_status = cnt;
pthread_exit((void*)ret_status);
}