c - 线程问题

标签 c pthreads

getvariana: tpp.c:63: __pthread_tpp_change_priority: Assertion `new_prio == -1 || (new_prio >= __sched_fifo_min_prio && new_prio <= __sched_fifo_max_prio)' failed.

大家好

我正在尝试重新运行一个创建 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);
}

最佳答案

这是您清理过的程序。它在没有上述断言的情况下运行:

#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);
}

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

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

  • test() , th未归零意味着任何失败pthread_create正在使用旧的线程引用。

  • tfunc ,你做了一个 pthread_join如果( g_count == value_needed ) ,但无论如何你都退出了,即你总是立即执行 pthread_join或等价物。注意我还测试了下面没有 sleep() 的版本,所以现在可以立即退出了。

  • 其他各种正字法问题。

  • 没有错误处理

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

如果您发布了一个实际导致问题的可编译代码的最小示例,我可能会进一步帮助您。

关于c - 线程问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21825291/

相关文章:

使用 pthreads 在 C 中将顺序循环转换为并行循环

c++ - pthread C++在Centos中无法运行

c - 如何从流中获取视频和音频并输出到视频文件和图像

c - 在c中绘制sin函数

c - 为什么 printf 有返回值?

c++ - synergy 1.4.16 编译失败 - 未定义对 `pthread_xxxx' 的引用

c - 如何以编程方式停止从标准输入读取?

c - 链接列表错误读取和错误消息

在 Mac OS X 上使用 SWIG 编译 Ruby 库

c - 不知道在多线程的情况下如何从终端获取命令