线程问题

Adi*_* Ya 7 c pthreads

getvariana:tpp.c:63:__ pthread_tpp_change_priority:断言`new_prio == -1 || (new_prio> = __sched_fifo_min_prio && new_prio <= __sched_fifo_max_prio)'失败.

大家好,

我试图重新运行一个创建5个线程的程序,在pthread_join()之后,我做了一个返回,基于此,我重新运行整个程序,即它在while(1)循环中.

当我第二次运行程序时,我会看到一个错误,如上所示.我无法追查它的起源.任何人都可以解释为什么会导致这个错误?

仅供参考:我不使用任何互斥锁或信号量.我等待线程加入,之后我重新运行整个程序.这与竞争条件有关吗?我假设,当我等待所有5个线程加入时,只有这样我才能离开pthread

main
{
    while(1)
    {
         test();
    }
}//main

test()
{
    for( i = 0; i < 5; i++ )
        pthread_create( &th[i], NULL, tfunc, &some_struct);

    for( i = 0; i < 5, i++ )
        pthread_join( th[i], NULL);
}

void * tfunc( void * ptr )
{
    // waiting for a callback function to set a global counter to a value
    // sleep until then
    if( g_count == value_needed )
        pthread_exit(NULL);
}
Run Code Online (Sandbox Code Playgroud)

abl*_*igh 6

这是您的程序已清理。它在没有以上声明的情况下运行:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>

static pthread_t th[5];

void *
tfunc (void *ptr)
{
  sleep (5);                    /* remove this to test it without the sleep */
  pthread_exit (NULL);
}

void
test ()
{
  int i;
  memset (th, 0, 5 * sizeof (pthread_t));

  for (i = 0; i < 5; i++)
    {
      if (pthread_create (&th[i], NULL, tfunc, NULL) < 0)
        perror ("pthread_create");
    }

  for (i = 0; i < 5; i++)
    {
      if (pthread_join (th[i], NULL) < 0)
        perror ("pthread_join");
    }
}

int
main (int argc, char **argv)
{
  while (1)
    {
      test ();
    }
  exit (0);
}
Run Code Online (Sandbox Code Playgroud)

这是我在清理时注意到的内容:

  • for( i = 0; i < 5, i++ ) 逗号不是分号意味着循环可能没有运行

  • 中的test()th未清零,表示任何失败都pthread_create在使用旧的线程引用。

  • 在中tfunc,您执行了pthread_joinif ( g_count == value_needed ),但是无论如何都退出了,即您总是立即执行the pthread_join或等效操作。请注意,我还在下面测试了不带的版本sleep(),因此立即退出即可。

  • 各种其他正字法问题。

  • 没有错误处理

由于存在一些编译问题,我怀疑您可能没有编译上面粘贴的代码,但是更加复杂。我怀疑这是导致问题的部分原因。

如果您发布了实际上导致问题的可编译代码的最小示例,那么我可能可以为您提供进一步的帮助。


Pei*_*Zhu 6

tpp.c:63: __pthread_tpp_change_priority: 断言 是一个已知问题并已解决:
https ://sourceware.org/ml/libc-help/2008-05/msg00071.html
简而言之,该问题是由重复锁定 a 引起的fast mutex,并通过使用 a 解决recursive mutex,并且默认pthread_mutex_t值不是递归的。pthread_mutex_t线程内部是否有可能深入运行代码??
顺便说一句,要使互斥锁递归,请使用属性设置互斥锁属性PTHREAD_MUTEX_RECURSIVE_NP

  • 当然,但 OP 说 _“仅供参考:我不使用任何互斥锁或信号量”_。如果他没有使用任何互斥锁或信号量,问题怎么可能是选择的互斥类型? (2认同)
  • @abligh 这就是为什么我问“在运行代码的线程内部是否可能存在 pthread_mutex_t 深处??”以澄清问题 (2认同)