Science, asked by gurvinderSingh1233, 1 year ago

C : Write a multi threaded program program in C language ?

Answers

Answered by jannatkhan51
2
POSIX program is a multi threaded program

Gaurav535: hii
Answered by sushiladevi4418
0

Answer:

Write a multithreaded program in C language.

Explanation:

#include <iostream>

#include <cstdlib>

#include <pthread.h>

using namespace std;

#define NUM_THREADS 5

void *PrintHello(void *threadid) {

  long tid;

  tid = (long)threadid;

  printf("Hello World! Thread ID, %d\n", tid);

  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 << endl;

     rc = pthread_create(&threads[i], NULL, PrintHello, (void *)i);

     if (rc) {

        printf("Error:unable to create thread, %d\n", rc);

        exit(-1);

     }

  }

  pthread_exit(NULL);

}

Output :-

main() : creating thread, 0

main() : creating thread, 1

main() : creating thread, 2

Hello World! Thread ID, 0

Hello World! Thread ID, 1

Hello World! Thread ID, 2

Similar questions