Skip to content

Commit 4884168

Browse files
committed
Lab1 completed
1 parent a8a4773 commit 4884168

File tree

1 file changed

+34
-15
lines changed

1 file changed

+34
-15
lines changed

lab.cpp

+34-15
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,51 @@
55

66
using namespace std;
77

8-
bool flag1 = true;
9-
bool flag2 = true;
10-
118
void* funcThread1(void* args) {
12-
while (flag1) {
13-
cout << '1';
14-
usleep(1000);
9+
cout << "\nThe First thread started. . .\n";
10+
int arg = *((int*) args);
11+
12+
while (*(bool*)args) {
13+
cout << '1' << flush;
14+
sleep(1);
1515
}
16+
pthread_exit((void*)150);
1617
}
1718

1819
void* funcThread2(void* args) {
19-
while (flag2) {
20-
cout << '2';
21-
usleep(1000);
20+
cout << "The Second thread started. . .\n";
21+
int arg = *((int*) args);
22+
23+
while (*(bool*) args) {
24+
cout << '2' << flush;
25+
sleep(1);
2226
}
27+
pthread_exit((void*)10);
2328
}
2429

2530
int main() {
2631
pthread_t thr1;
32+
int statusThr1 = 1;
33+
bool flag1 = true;
34+
2735
pthread_t thr2;
28-
pthread_create(&thr1, NULL, funcThread1, NULL);
29-
pthread_create(&thr2, NULL, funcThread2, NULL);
36+
int statusThr2 = 1;
37+
bool flag2 = true;
38+
39+
40+
pthread_create(&thr1, NULL, funcThread1, &flag1);
41+
42+
pthread_create(&thr2, NULL, funcThread2, &flag2);
43+
44+
cout << "Press any key (ENTERR) to stop it\n\n";
45+
3046
getchar();
31-
flag1 = false;
32-
flag2 = false;
33-
pthread_join(thr1, NULL);
34-
pthread_join(thr2, NULL);
47+
flag1 = 0;
48+
flag2 = 0;
49+
pthread_join(thr1, (void**)&statusThr1);
50+
pthread_join(thr2, (void**)&statusThr2);
51+
cout << "\nThe First thread ended with " << statusThr1 << " code";
52+
cout << "\nThe Second thread ended with " << statusThr2 << " code";
53+
cout << "\nEnd of this shit\n";
3554
return 0;
3655
}

0 commit comments

Comments
 (0)