在学习有关pthread_cleanup_push的概念时,我观察到pthread_exit()和pthread_cancel()对pthread_cleanup_pop()的影响不同。下面是代码示例。

void push_routine_1(void * arg) {
    printf(" Push Routine 1\n");
}

void push_routine_2(void * arg) {
    printf(" Push Routine 2\n");
}

void push_routine_3(void * arg) {
    printf(" Push Routine 3\n");
}

void push_routine_4(void * arg) {
    printf(" Push Routine 4\n");
}

void * thread_routine(void * arg) {
    pthread_cleanup_push(push_routine_1, NULL);
    pthread_cleanup_push(push_routine_2, NULL);
    pthread_cleanup_push(push_routine_3, NULL);
    pthread_cleanup_push(push_routine_4, NULL);

    pthread_exit(NULL);

    pthread_cleanup_pop(1);
    pthread_cleanup_pop(1);
    pthread_cleanup_pop(1);
    pthread_cleanup_pop(1);
}

上面的程序给出了这样的输出:
Push Routine 4
Push Routine 3
Push Routine 2
Push Routine 1

当使用pthread_cancel(pthread_self())而不是pthread_exit(NULL)时,输出如下
Push Routine 3
Push Routine 2
Push Routine 1

使用push_routine_4()时不执行pthread_cancel()的原因是什么?

最佳答案

我认为您可能不会认为pthread_cancel(pthread_self())pthread_exit(NULL)完全相同。
POSIX不会告诉我pthread_cancel(pthread_self())应该做什么。但我注意到它说:

“任何其他其他线程”确实使我感到奇怪。但是我不认为这是问题所在。
默认的“取消类型”为PTHREAD_CANCEL_DEFERRED。因此,pthread_cancel()直到已取消线程中的下一个“取消点”才会生效。因此,您的代码将继续到第一个pthread_cleanup_pop(1),它不是“取消点”,然后是printf(" Push Routine 4\n"),它可能是“取消点”。
我调整了您的代码,如下所示:

static volatile unsigned step ;

void push_routine_1(void * arg) {
  step += 10 ;
  printf(" Push Routine 1 -- %u\n", step);
  step += 10 ;
}

void push_routine_2(void * arg) {
  step += 100 ;
  printf(" Push Routine 2 -- %u\n", step);
  step += 100 ;
}

void push_routine_3(void * arg) {
  step += 1000 ;
  printf(" Push Routine 3 -- %u\n", step);
  step += 1000 ;
}

void push_routine_4(void * arg) {
  step += 10000 ;
  printf(" Push Routine 4 -- %u\n", step);
  step += 10000 ;
}

void * thread_routine(void * arg)
{
  unsigned frag = *(unsigned*)arg ;

  printf("Start %u\n", frag) ;

  step = 0 ;
  pthread_cleanup_push(push_routine_1, NULL); step += 10 ;
  pthread_cleanup_push(push_routine_2, NULL); step += 100;
  pthread_cleanup_push(push_routine_3, NULL); step += 1000 ;
  pthread_cleanup_push(push_routine_4, NULL); step += 10000 ;

  step++ ;
  if (frag == 0)
    pthread_exit(NULL) ;        // 11111
  else
    pthread_cancel(pthread_self()) ;

  step++ ;                      // 11112

  if (frag == 2)
    printf(" Continue %u\n", step) ;

  step++ ;                      // 11113
  pthread_cleanup_pop(1);       // (4)
  step++ ;                      // xxxx4
  pthread_cleanup_pop(1);       // (3)
  step++ ;                      // xxxx5
  pthread_cleanup_pop(1);       // (2)
  step++ ;                      // xxxx6
  pthread_cleanup_pop(1);       // (1)
  step++ ;                      // xxxx7
}

int
main(Unused int argc, Unused char* argv[])
{
  unsigned frag ;

  for (frag = 0 ; frag < 3 ; ++frag)
    {
      pthread_t thr ;

      pthread_create(&thr, NULL, thread_routine, &frag) ;
      pthread_join(thr, NULL) ;

      printf(" Finally %u\n", step) ;
    } ;

  return 0 ;
}
结果是:
Start 0
 Push Routine 4 -- 21111
 Push Routine 3 -- 32111
 Push Routine 2 -- 33211
 Push Routine 1 -- 33321
 Finally 33331
Start 1
 Push Routine 4 -- 21113
 Push Routine 3 -- 22113
 Push Routine 2 -- 23213
 Push Routine 1 -- 23323
 Finally 23333
Start 2
 Continue 11112
 Push Routine 4 -- 21112
 Push Routine 3 -- 32112
 Push Routine 2 -- 33212
 Push Routine 1 -- 33322
 Finally 33332
因此对于:
  • 'Start 0'我们看到(如预期的那样)线程的前进不超过pthread_exit(NULL)
  • '开始1'我们看到线程到达pthread_cleanup_pop(1); // (4),但是push_routine_4()没有超过printf()
    我相信这表明printf()完成后便开始了取消过程。
  • '开始2'我们看到push_routine_4()printf(" Continue %u\n", step)之后,之后的step++之前被调用。
    我相信这表明printf(" Continue ...)完成后便开始了取消过程。

  • 在我的机器上,运行glibc 2.30的Linux 5.6.8在功能完成后似乎printf()是一个“取消点”。您得到的效果表明printf()在打印任何内容之前是一个“取消点”? [或者还有其他原因导致printf()中的push_routine_4()不产生任何输出。]

    关于c - 在Linux中,pthread_exit()和pthread_cancel()如何调用清理例程?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61867053/

    10-11 00:38
    查看更多