-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpthread.cpp
More file actions
37 lines (28 loc) · 829 Bytes
/
pthread.cpp
File metadata and controls
37 lines (28 loc) · 829 Bytes
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
#include <iostream>
#include <cstdlib>
#include <pthread.h>
using namespace std;
#define NUM_THREADS 4
void *PrintHello(void *threadid)
{
long tid;
//tid = *(long*)threadid;
//cout << "Hello World! Thread ID, " << tid << endl;
cout << "Hello World! Thread " << std::flush << endl;
//pthread_detach(pthread_self()); // Detach the current thread
pthread_exit(NULL);
}
int main () {
pthread_t threads[NUM_THREADS];
int rc;
int i;
for( i = 0; i < NUM_THREADS; i++ ) {
cout << "main() : creating thread, " << i << std::flush << endl;
rc = pthread_create(&threads[i], NULL, PrintHello, (void *)i);
if (rc) {
cout << "Error:unable to create thread," << rc << endl;
exit(-1);
}
}
pthread_exit(NULL);
}