Hi friends,
when I am passing arguments to main, I want another function to be able to have access to that function, the problem is that I am creating athread, which has a function like void *xyz(void *), how can pass the refernce of argv[x] to this function, if you see my program, you will better see the problem, so here it is!
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <pthread.h>
int main(int argc, char *argv[])
{
void *thread1(void *);
void *thread2(void *);
if(argc != 2)
{
printf("Invalid no. of arguments!\n");
exit(-1);
}
else
{
pthread_t tid1;
pthread_t tid2;
int t1 = 1;
int t2 = 2;
pthread_attr_t attr1;
pthread_attr_t attr2;
pthread_attr_init(&attr1);
pthread_attr_init(&attr2);
{
pthread_create(&tid1,&attr1,thread1,(void *)t1);
pthread_create(&tid2,&attr2,thread2,(void *)t2);
}
{
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
}
printf("\nThreads finished!\n");
}
return 0;
}
void *thread1(void *n)
{
while(1)
{
printf("%s\n\n",argv[1]);
sleep(1);
}
pthread_exit(0);
}
void *thread2(void *n)
{
while(1)
{
printf("%s\n\n",argv[2]);
sleep(1);
}
pthread_exit(0);
}
I this program the two thread functions don't have access to arv, what should I do???
Thanks