pthreads c 2b 2b example

Solutions on MaxInterview for pthreads c 2b 2b example by the best coders in the world

showing results for - "pthreads c 2b 2b example"
Jerónimo
23 Mar 2017
1#include <iostream>
2#include <cstdlib>
3#include <pthread.h>
4
5using namespace std;
6
7#define NUM_THREADS 5
8
9void *PrintHello(void *threadid) {
10   long tid;
11   tid = (long)threadid;
12   cout << "Hello World! Thread ID, " << tid << endl;
13   pthread_exit(NULL);
14}
15
16int main () {
17   pthread_t threads[NUM_THREADS];
18   int rc;
19   int i;
20   
21   for( i = 0; i < NUM_THREADS; i++ ) {
22      cout << "main() : creating thread, " << i << endl;
23      rc = pthread_create(&threads[i], NULL, PrintHello, (void *)i);
24      
25      if (rc) {
26         cout << "Error:unable to create thread," << rc << endl;
27         exit(-1);
28      }
29   }
30   pthread_exit(NULL);
31}