Please write a multi-thread program using Pthreads library. In this problem, 10 thread should be created and the counter integer variable is shared among threads and initialized by 0.
In addition, each thread executes an infinite loop which keeps increasing the counter value by 1 and prints counter and thread id values in each iteration
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
int counter= 0;
void *myThreadFun(void *vargp)
{
int *myid = (int *)vargp;
while(1)
{
printf("Thread ID: %d, Counter Value : %d\n",
*myid,++counter);
}
}
int main()
{
int i;
pthread_t tid;
for (i =1; i <=10; i++)
{ pthread_create(&tid, NULL,
myThreadFun, (void *)&i);
}
for (i =1; i <=10; i++)
{ pthread_join(tid,NULL);
}
pthread_exit(NULL);
return 0;
}
////USE THIS CODE FOR CHECKING THE CORRECTNESS OF OUTPUT (RUN MULTIPLE TIMES TO SEE ALL ///THREADS INVOLVED IN RUNNING
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
int counter= 0;
void *myThreadFun(void *vargp)
{
int *myid = (int *)vargp;
while(counter<300)
{
printf("Thread ID: %d, Counter Value : %d\n",
*myid,++counter);
}
}
int main()
{
int i;
pthread_t tid;
for (i =1; i <=10; i++)
{ pthread_create(&tid, NULL,
myThreadFun, (void *)&i);
}
for (i =1; i <=10; i++)
{ pthread_join(tid,NULL);
}
pthread_exit(NULL);
return 0;
}
Please write a multi-thread program using Pthreads library. In this problem, 10 thread should be created...