0% found this document useful (0 votes)
2 views1 page

Week 6

The document contains a C program that demonstrates the use of threads using the pthread library. It defines two thread functions, mythread1 and mythread2, which print numbers from 1 to 10 and messages indicating the start and end of each thread. The main function creates two threads and waits for their completion, but it incorrectly uses the same thread ID for both threads, which may lead to undefined behavior.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

Week 6

The document contains a C program that demonstrates the use of threads using the pthread library. It defines two thread functions, mythread1 and mythread2, which print numbers from 1 to 10 and messages indicating the start and end of each thread. The main function creates two threads and waits for their completion, but it incorrectly uses the same thread ID for both threads, which may lead to undefined behavior.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include<stdio.

h>
#include<stdlib.h>
#include<pthread.h>

void *mythread1(void *vargp)


{
int i;
printf("thread1\n");
for(i=1;i<=10;i++)
printf("i=%d\n",i);
printf("exit from thread1\n");
return NULL;
}

void *mythread2(void *vargp)


{
int j;
printf("thread2 \n");
for(j=1;j<=10;j++)
printf("j=%d\n",j);

printf("Exit from thread2\n");


return NULL;
}

int main()
{
pthread_t tid;
printf("before thread\n");
pthread_create(&tid,NULL,mythread1,NULL);
pthread_create(&tid,NULL,mythread2,NULL);
pthread_join(tid,NULL);
pthread_join(tid,NULL);
exit(0);
}

You might also like