ConiC
ConiC

Reputation: 13

Use a pipe in a thread function

I have to do a function that contains two threads. One has to read one pipe and the other has to write in another pipe. But I dont know how to pass the pipe yo the thread function, because there is another structure that has to go in the arguments part of the pthread_create. I'll put some code if it's a little clearer this way.

void *readFather(Job *job){
    write (desc1Pipe[0],job->in, job->size_in);
}

void *writeFather(Job *job){
if ((job->size=read (desc2Pipe[1], job->out,  job->max_out) > job->max_out) 
    kill(job->pidhijo,SIGKILL);                        
}

Those are the functions I give to pthread_create.

This is the calling of the threads.

pthread_t pid1; 
pthread_t pid2;
...
pthread_create(&pid1, NULL, readFather, &job);      
pthread_create(&pid2, NULL, writeFather, &job);

Please help me, I've done all I can think of, I'm really new to all C related stuff.

Upvotes: 1

Views: 307

Answers (1)

John Kugelman
John Kugelman

Reputation: 361809

If you have multiple pieces of data you can wrap them in a struct.

struct readData
{
    Job *job;
    int  fd;
};

...

struct readData readData;

readData.job = job;
readData.fd  = pipeFd;

pthread_create(&pid1, NULL, readFather, &readData);

Upvotes: 2

Related Questions