hello im new to linux programming and i am experiencing a bit of diffuclty with pthreads...basically the pthread_create() function
in my code below i tend to pass an integer variable as the argument for my runner() function but i get a warning and an error..i have tried several ways of passing this argument but im not very good with pointers...a bit of help in my code would be appreciated..
#include<pthread.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<semaphore.h>
#include<unistd.h>
pthread_t t1,t2;
pthread_attr_t tattr;
int counter;
sem_t mutex;
void runner(void *arg)
{
printf("Thread received %i",*arg);
int i=0;
for(i=0;i<5;i++)
{
sem_wait(&mutex);
printf("Thread %i says %i",*((int *)arg),counter);
counter++;
sem_post(&mutex);
sleep(3);
}
}
int main()
{
int r;
printf("hi");
r=sem_init(&mutex,0,1);
r=pthread_attr_init(&tattr);
int t=1;
r=pthread_create(&t1,&tattr,runner,(void *)&t);
t=2;
r=pthread_create(&t2,&tattr,runner,(void *)&t);
printf("Threads initialised");
r=pthread_join(t1,NULL);
r=pthread_join(t2,NULL);
r=sem_destroy(&mutex);
return 0;
}