Tuesday, August 14, 2012

C Program to create threads!!

Below is the simple C program to create the thread.
#include<stdio.h>
#include<pthread.h>

void *print_data(void *str)
{
    char *msg = (char *)str;
    printf("given message is '%s'\n",msg);
}
main()
{
    pthread_t th1,th2;
    int ret1,ret2;
    char msg1[]="this is first message";
    char msg2[]="this is second  message";

    ret1 = pthread_create(&th1,NULL, print_data,(void *)msg1);
    ret2 = pthread_create(&th2,NULL, print_data,(void *)msg2);

    printf("th1 return value is %d\n",ret1);
    printf("th2 return value is %d\n",ret2);

    pthread_join(th1,NULL);
    pthread_join(th2,NULL);
}

No comments:

Popular Posts