how to implement threads in c
Pthreads are a simple and effective way of creating a multi-threaded application. 
When a thread is created using 
pthread_create, both the 
original thread and the new thread share the same code base and the same
 memory – it’s just like making two function calls at the same time.
All C programs using pthreads need to include the pthread.h header file
.#include<stdio.h>
#include<pthread.h>
pthread_t thread[2];
static void *function1(void *_){
 int i;
 for(i=0;i<10;i++){
  printf("\nThread1 says=%d",i);
  sleep(1);
}
}
static void *function2(void *_){
 int i;
 for(i=0;i<5;i++){
  printf("\nThread2 says=%d",i);
  sleep(5);
}
}
int main(){
    pthread_create(&thread[0], NULL , function1, NULL);
    pthread_create(&thread[1], NULL , function2, NULL);
    pthread_exit(NULL);
return 1;
}  
 
0 comments:
Post a Comment