-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpthread-example.c
100 lines (80 loc) · 2.03 KB
/
pthread-example.c
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
#include <iostream>
#include "pthread.h"
#include <string>
#include <unistd.h>
#include <sstream>
using namespace std;
string int_to_str(int x) {
stringstream ss;
ss << x;
return ss.str();
}
#define NUM_THREADS 5
#define BLACK "\033[0m"
#define RED "\033[1;31m"
#define GREEN "\033[1;32m"
#define YELLOW "\033[1;33m"
#define BLUE "\033[1;34m"
#define CYAN "\033[1;36m"
static pthread_mutex_t mutex;
void* PrintAsciiText(void *id)
{
string colour;
pthread_mutex_lock(&mutex);
switch((long)id)
{
case 0:
colour = RED;
break;
case 1:
colour = GREEN;
break;
case 2:
colour = YELLOW;
break;
case 3:
colour = BLUE;
break;
case 4:
colour = CYAN;
break;
default:
colour = BLACK;
break;
}
cout << colour << "I'm a new thread, I'm number " << (long)id << BLACK << endl;
pthread_mutex_unlock(&mutex);
char str[] = "pthread_yield() of thread() ";
int sleeping = rand() % 20 + 1;
string concat_str;
concat_str = str + int_to_str((long)id);
const char* myCharArr = concat_str.c_str();
int err = pthread_yield();
perror(myCharArr);
cout << colour << "2nd message, sleeping for " << colour << sleeping << colour << " seconds. I'm thread number " << (long)id << BLACK << endl;
sleep(sleeping);
cout << colour << "3rd message, I'm thread number " << (long)id << BLACK << endl;
pthread_exit(NULL);
}
int main()
{
pthread_t threads[NUM_THREADS];
for (long int i = 0 ; i < NUM_THREADS ; ++i)
{
int t = pthread_create(&threads[i], NULL, PrintAsciiText, (void*)i);
if (t != 0)
{
cout << "Error in thread creation: " << t << endl;
}
}
for(int i = 0 ; i < NUM_THREADS; ++i)
{
void* status;
int t = pthread_join(threads[i], &status);
if (t != 0)
{
cout << "Error in thread join: " << t << endl;
}
}
return 0;
}