Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ pthread在分离的线程上创建错误11_C++_C_Linux_Multithreading_Pthreads - Fatal编程技术网

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

C++ pthread在分离的线程上创建错误11,c++,c,linux,multithreading,pthreads,C++,C,Linux,Multithreading,Pthreads,我有一个服务器应用程序,它等待队列,获取传入消息,并生成一个线程来处理接收到的消息并发送回复 我使用的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 (v

我有一个服务器应用程序,它等待队列,获取传入消息,并生成一个线程来处理接收到的消息并发送回复

我使用的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);
class MessageProcessor
{
    MessageProcessor();
    static void * MessageProcessor::process(void * arg);
}

void * MessageProcessor::process(void * arg)
{
    // do something here with arg
}
每次我运行这个程序时,显示的错误代码都是11,根据我在互联网上读到的内容,这显然表明我的进程已经超过了最大线程数阈值

但是,

  • 这是从一开始就发生的,而不是在我运行应用程序一段时间之后
  • 线程是分离创建的,因此我不必使用pthread_join()
  • 我使用
    top
    以及
    ps-p-lfT
    来检查应用程序使用了多少线程,只有3个线程(一个用于主服务器,一个用于消息接收方,一个用于队列系统的消息发送方)
PS

“流程”原型如下所示:

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);
class MessageProcessor
{
    MessageProcessor();
    static void * MessageProcessor::process(void * arg);
}

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

errno 11通常是EAGAIN,在这种情况下,它意味着没有更多的进程可用(linux将线程视为轻量级进程-请参阅克隆手册页)

while(true)
循环将永远运行


注意:如果您有一个特殊版本的Linux,比如ARM,则不需要输入错误号11。因此,对这个答案持保留态度。

errno 11通常是EAGAIN,在这种情况下,它意味着不再有可用的进程(linux将线程视为轻量级进程-请参阅克隆手册页)

while(true)
循环将永远运行


注意:如果您有一个特殊版本的Linux,比如ARM,则不需要输入错误号11。因此,对这个答案持保留态度。

与所有pthreads函数一样,不会将
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规定如下:

errno的值只能在调用明确声明为其设置的函数后定义[…]只有当函数的返回值指示errno的值有效时,才应检查errno的值


由于
pthread\u create
未记录为设置
errno
,这意味着调用
pthread\u create
后未定义该值,因此不应检查该值。

与所有pthreads函数一样,不会将
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规定如下:

errno的值只能在调用明确声明为其设置的函数后定义[…]只有当函数的返回值指示errno的值有效时,才应检查errno的值


由于
pthread\u create
未记录为设置
errno
,这意味着调用
pthread\u create
后未定义该值,因此不应检查该值。

您的代码使用的是未初始化的
子属性,您必须执行以下操作:

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

您的代码正在使用未初始化的
子属性
,您必须执行以下操作:

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

假设“显示的错误代码”实际上是指“发送到进程的信号”,信号11是SIGSEGV,这是一种分段冲突,通常是由于指针使用不当造成的。如果11实际上是
errno
变量的报告值,则很可能是EAGAIN,这通常表示调用暂时失败,应在以后重试。@twalberg所显示的错误代码是指在printf()中我的代码中打印的错误,即
errno
。在发送有效回复之前,每分钟会收到一条消息,持续10分钟,因此会进行重试。但是,每次收到消息时,错误总是11。您是否有机会更改堆栈大小?你有很多线程本地存储数据吗?EAGAIN表示的只是资源短缺,而不是具体的资源。线程分配需要:新进程/线程、堆栈的内存和TLS段的副本。我不更改堆栈大小,我得到的最大线程本地数据通常是消息或它的回复,其大小可以达到4-5 KB,但不能超过。
pthread\u attr\u setdetachstate(&child\u attr,pthread\u CREATE)
应该是
pthread\u attr\u init(&child\u attr);pthread_attr_setdetachstate(&child_attr,pthread_CREATE_detachstate)
,您实际在做什么?您是否初始化了
子属性
?如果未将分离状态正确设置为PTHREAD_CREATE_DETACHED,则代码存在资源泄漏。假设“显示的错误代码”实际上是指“传递给进程的信号”,则信号11是SIGSEGV,这是一种分段冲突,通常是由于指针使用不当造成的。如果11实际上是
errno
变量的报告值,则很可能是EAGAIN,这通常表示调用暂时失败,应在以后重试。@twalberg所显示的错误代码是指在printf()中我的代码中打印的错误,即
errno
。在发送有效回复之前,每分钟会收到一条消息,持续10分钟,因此会进行重试。但是,每次收到消息时,错误总是11。您是否有机会更改堆栈大小?你有很多线程本地存储数据吗?EAGAIN表示的只是资源短缺,而不是具体的资源。线程分配需要:新进程/线程、堆栈的内存和TLS段的副本。我不更改堆栈大小,我得到的最大线程本地数据通常是消息或它的回复,其大小可以达到4-5 KB,但不能超过。
pthread\u attr\u setdetachstate(&child\u attr,pthread\u CREATE)应该是
pthread\u attr\u init(&child\u attr)