The POSIX threads API allows you to pass a generic void*
for user data when your thread function is first called.
Since you already have the following structure defined:
struct Pipefd {
int tuberia1[2];
int tuberia2[2];
};
Rather than casting a single field of this structure to void*
you might want to pass a pointer to the actual structure:
void* ppf = reinterpret_cast<void *>(&pf);
pthread_create(NULL,NULL, listenProcess,ppf);
Now, your modified thread function would look like this:
void *listenProcess(void* x){
Pipefd* ppf = reinterpret_cast<Pipefd *>(x);
close(0);
dup(ppf->tuberia2 [0]);
string word;
while(getline(cin,word)){
cout << "Termino y llego: " << word << endl;
}
}
UPDATE:
You also had an invalid call to pthread_create (...)
, you must pass it a pointer to a pthread_t
variable. This should fix your segmentation fault caused by calling pthread_create (...)
:
void* ppf = reinterpret_cast<void *>(&pf);
pthread_t tid;
pthread_create(&tid,NULL, listenProcess,ppf);