简体   繁体   English

Pthread_create错误11只有5个并发线程

[英]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. 我的程序有4个始终在这里的线程。 We will name these thread from 1 to 4. 我们将从1到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. 线程号4用于将消息发送到外围设备。

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); 这将创建一个线程来侦听套接字上的响应,当套接字不再包含任何消息时,该线程将返回:例如,如果iRet = recv(m_iSocket, pcRecBuf, DEFAULT_READ_DATA_LEN, 0); makes iRet take the value 0. 使iRet取值为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. 这里的问题是几个小时后,pthread create将崩溃,并显示错误11。我在堆栈溢出中看到这意味着系统可能没有足够的资源或太多的线程。

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. 但是我不了解QtCreator调试中的情况,我只能看到从1到4的线程。我知道我可能已经创建了300个线程,但是线程列表仅包含4个,这意味着所有其他线程都被终止了。

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. 您应该调用pthread_join释放获得的资源。 Or you can use attribute PTHREAD_CREATE_DETACHED in that case you'll not require to use pthread_join 或者在这种情况下,您可以使用属性PTHREAD_CREATE_DETACHED ,而无需使用pthread_join

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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