简体   繁体   中英

Pthread_create error 11 with only 5 simultaneous thread

I have a problem in a multi threaded program. My program has 4 threads that are always here. We will name these thread from 1 to 4.

The goal for my program is to communicate on a socket with a peripheral. Thread number 4 is used to send the messages to the peripheral.

Each time a message is sent to the peripheral, I use :

pthread_create(&m_hThreadMsgReader, NULL, &ThreadMsgReader, (void*) &argStruct);

This will create a thread to listen to the response on the socket, this thread will return when the socket doesn't contain anymore message : ie if iRet = recv(m_iSocket, pcRecBuf, DEFAULT_READ_DATA_LEN, 0); makes iRet take the value 0.

So with the current implementation, a data pooling is made (every minute) on the peripheral (aside from other command sent via user input).

The problem here is that after a few hours, pthread create will crash with an error 11. I've seen on stack overflow that it means that the system might not have enough resources or too many threads.

But I don't understand as in QtCreator debug, i can only see the thread from 1 to 4. I know I might have created like 300 threads, but the list of thread only contains 4, which means all other threads were terminated.

So I don't really understand if the maximum number of thread creation is during the whole lifetime of the process or the number of thread present at the same time.

Should I just find a way to have a single thread for the listening even if it will listen on a empty socket ? Is my implementation a bad pattern ?

You should call pthread_join to free the resources acquired. Or you can use attribute PTHREAD_CREATE_DETACHED in that case you'll not require to use pthread_join

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM