Frage

I'm building 2 programs (client/server) that communicate through FIFOs. Both programs have threads. When the Client's thread ends it doesn't get joined, and main hangs.

The programs do the following:

Server:

  1. main: reads from FIFO1
  2. main: create thread to process request.
  3. main: goto 1
  4. thread: processes request
  5. thread: sends response to FIFO2
  6. thread: exit

Client:

  1. main: spawn thread
  2. thread: reads X responses to client from FIFO2
  3. thread: exit
  4. main: sends X requests to FIFO1
  5. main: wait thread to exit
  6. main: check responses

The server works well and all threads are joined accordingly.

The client fails in step 5. Using

pthread_join(&reader,NULL);

hangs main forever. I've checked, and the thread already ended.

Using

pthread_tryjoin_np(&reader,NULL);

I get

errorcode=16

strerror gives

Device or resource busy

Creating the thread with:

pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_JOINABLE);
pthread_create(&reader,&attr,trataResp,NULL);

Or with: pthread_create(&reader,NULL,trataResp,NULL);

doesn't produce any change.

How can I resolve this?

War es hilfreich?

Lösung

Well pthread_join receives the thread id, not the address for it. This line:

pthread_join(&reader,NULL);

Should be:

pthread_join(reader,NULL);

If reader was declared as pthread_t.

Hope it's not just a typo in your question and that this actually helps.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top