Tuesday, February 2, 2010

Pthread Part-1

A Simple Pthread Program:
    0 #include <stdio.h>
1 #include <unistd.h>
2 #include <pthread.h>
3
4 void *fun(void *args) {
5 int i;
6
7 for (i=0; i<10; i++) {
8 printf("hello world\n");
9 sleep(1);
10 }
11 pthread_exit(NULL);
12 }
13
14 int main() {
15 pthread_t pt_id;
16
17 printf("Main: Before Create\n");
18 pthread_create(&pt_id, NULL, fun, NULL);
19 printf("Main: After Create\n");
20 pthread_join(pt_id, NULL);
21 printf("Main: After Join\n");
22 pthread_exit(NULL);
23 return 0;
24 }
25


Output:
  
$./pthread1
Main: Before Create
Main: After Create
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
Main: After Join
$