5
5
6
6
using namespace std ;
7
7
8
- bool flag1 = true ;
9
- bool flag2 = true ;
10
-
11
8
void * funcThread1 (void * args) {
12
- while (flag1) {
13
- cout << ' 1' ;
14
- usleep (1000 );
9
+ cout << " \n The First thread started. . .\n " ;
10
+ int arg = *((int *) args);
11
+
12
+ while (*(bool *)args) {
13
+ cout << ' 1' << flush;
14
+ sleep (1 );
15
15
}
16
+ pthread_exit ((void *)150 );
16
17
}
17
18
18
19
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 );
22
26
}
27
+ pthread_exit ((void *)10 );
23
28
}
24
29
25
30
int main () {
26
31
pthread_t thr1;
32
+ int statusThr1 = 1 ;
33
+ bool flag1 = true ;
34
+
27
35
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
+
30
46
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 << " \n The First thread ended with " << statusThr1 << " code" ;
52
+ cout << " \n The Second thread ended with " << statusThr2 << " code" ;
53
+ cout << " \n End of this shit\n " ;
35
54
return 0 ;
36
55
}
0 commit comments