简体   繁体   English

pthread在分离的线程上创建错误11

[英]pthread create Error 11 on detached threads

I have a server application, which waits on a queue, fetches incoming messages, and spawns a thread to process the received message and send a reply. 我有一个服务器应用程序,它在队列上等待,获取传入的消息,并产生一个线程来处理接收到的消息并发送回复。

The pthread portion/options I am using are as follows: 我正在使用的pthread部分/选项如下:

pthread_attr_t child_attr;
pthread_attr_setdetachstate(&child_attr, PTHREAD_CREATE);

// other code here

while (true)
{

    // code here to wait on valid message (msg)
    if (valid_message(msg))
    {
        pthread_t child_thread;
        MessageProcessor * processor = new MessageProcessor();
        if (0 == pthread_create(&child_thread, &child_attr, processor->process, (void *) msg))
        {
            printf("Thread dispatch successful\n");
        }
        else
        {
            printf("Error %d: could not create thread\n", errno);
        }
    }
}

// other code here
pthread_attr_destroy(&child_attr);

Every time I run this, the error code displayed is 11 , which apparently would indicate that my process has crossed the max threads threshold, based on stuff I've read on the Internet. 每次运行此命令时,显示的错误代码均为11 ,这显然表明我的进程已经超过了最大线程数阈值,这是基于我在Internet上阅读的内容。

However, 然而,

  • This is happening right from the beginning, not after I have run the application for a while. 这是从一开始就发生的,而不是在我运行应用程序一段时间之后发生的。
  • The threads are created detached, so I shouldn't have to use pthread_join(). 线程是分离创建的,因此我不必使用pthread_join()。
  • I used top as well as ps -p <PID> -lfT to check how many threads were in use by the application, and only 3 were (one for main server, one for the message receiver, and one for a message sender for the queue system) 我使用top以及ps -p <PID> -lfT来检查应用程序使用了多少个线程,只有3个线程(一个用于主服务器,一个用于消息接收器,一个用于消息发送器)。队列系统)

PS : PS

The "process" prototype is as follows: “过程”原型如下:

class MessageProcessor
{
    MessageProcessor();
    static void * MessageProcessor::process(void * arg);
}

void * MessageProcessor::process(void * arg)
{
    // do something here with arg
}

Like all pthreads functions, pthread_create does not set errno to report errors, it returns an error number instead. 像所有pthreads函数一样, pthread_create不会将errno为报告错误,而是返回错误号。 To see why it failed you need to print the return value, not errno . 要查看失败的原因,您需要打印返回值,而不是errno

const int err = pthread_create(&child_thread, &child_attr, processor->process, (void *) msg);

if (err == 0)
  printf("Thread dispatch successful\n");
else
  printf("Error %d: could not create thread\n", err);

POSIX specifies errno like so: POSIX这样指定errno

The value of errno shall be defined only after a call to a function for which it is explicitly stated to be set [...] The value of errno should only be examined when it is indicated to be valid by a function's return value. errno的值应定义只为它明确规定要设置的函数调用后[...]应该只当它表明通过一个函数的返回值有效的检查errno的值。

Since pthread_create is not documented to set errno it means the value is not defined after a call to pthread_create and should not be examined. 由于未记录pthread_create来设置errno因此这意味着在调用pthread_create之后未定义该值,因此不应检查该值。

errno 11 is usually EAGAIN, in this case it means no more processes (linux treats threads as light weight processes - see the clone manual page)available. errno 11通常是EAGAIN,在这种情况下,它意味着没有更多的进程可用(Linux将线程视为轻量级进程-请参见克隆手册页)。

The while(true) loop will run forever making processes. while(true)循环将永远使制作过程运行。

Note if you have a special version of Linux like ARM the error number 11 is NOT required to be EAGAIN. 请注意,如果您有特殊版本的Linux(如ARM),则错误号11不需要是EAGAIN。 So take this answer with a grain of salt. 因此,用一粒盐来回答这个问题。

Your code is using an uninitialized child_attr , you'll have to do: 您的代码正在使用未初始化的child_attr ,您必须执行以下操作:

  pthread_attr_init(&child_attr); 
  pthread_attr_setdetachstate(&child_attr, PTHREAD_CREATE_DETACHED);

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

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