【问题标题】:Segfault in pthread_joinpthread_join 中的段错误
【发布时间】:2016-06-25 21:04:05
【问题描述】:

我有一个相当简单的 C 代码:

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

void* run(void *arg) {
  printf("hello from %d!\n", *(int*)arg);
  pthread_exit(NULL);
}

int main() {
  int ids[4];
  pthread_t threads[4];

  for (int i = 0; i < 4; i++) {
    ids[i] = i;
    printf("create thread %d\n", i);
    threads[i] = pthread_create(&threads[i], NULL, run, (void*)&ids[i]);
  }

  for (int i = 0; i < 4; i++) {
    printf("waiting for %d to join...\n", i);
    pthread_join(threads[i], NULL);
    printf("thread %d joined\n", i);
  }

  pthread_exit(NULL);
  return 0;
}

我用它编译

$ gcc -pthread -g -o main main.c

我的输出是

$ ./main                                                                                                                                                                    
create thread 0
create thread 1
create thread 2
create thread 3
waiting for 0 to join...
hello from 0!
zsh: segmentation fault (core dumped)  ./main

如您所见,它存在段错误。 gdb 没有告诉我任何有用的信息:

$ gdb ./main                                                                                                                                                                
GNU gdb (GDB) 7.11
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-pc-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./main...done.
(gdb) r
Starting program: /home/picard/projects/bme/pp-lab/sudoku/main 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/libthread_db.so.1".
create thread 0
[New Thread 0x7ffff781a700 (LWP 9660)]
hello from 0!
create thread 1
[New Thread 0x7ffff7019700 (LWP 9661)]
create thread 2
hello from 1!
[New Thread 0x7ffff6818700 (LWP 9662)]
create thread 3
hello from 2!
[New Thread 0x7ffff6017700 (LWP 9663)]
hello from 3!
waiting for 0 to join...

Thread 1 "main" received signal SIGSEGV, Segmentation fault.
0x00007ffff7bc45e0 in pthread_join () from /usr/lib/libpthread.so.0
(gdb) bt
#0  0x00007ffff7bc45e0 in pthread_join () from /usr/lib/libpthread.so.0
#1  0x0000000000400786 in main () at main_old.c:22

以下是关于我的系统的一些信息:

$ gcc --version                                                                                                                                                             
gcc (GCC) 5.3.0
$ uname -a                                                                                                                                                                  
Linux enterprise 4.4.5-1-ARCH #1 SMP PREEMPT Thu Mar 10 07:38:19 CET 2016 x86_64 GNU/Linux

我应该如何继续调试这个问题?

【问题讨论】:

    标签: c linux multithreading pthreads


    【解决方案1】:

    pthread_create 成功返回0,而不是线程ID,所以

    threads[i] = pthread_create(&threads[i], NULL, run, (void*)&ids[i]);
    

    应该是

    if (pthread_create(&threads[i], NULL, run, (void*)&ids[i]))
    {
      //take care of the error
    }
    

    【讨论】:

    • 当然,记得检查返回码是否有错误;)。
    • 哇,确实,我不知道我的眼睛怎么错过了那个糟糕的任务
    猜你喜欢
    • 1970-01-01
    • 2018-11-06
    • 2015-05-02
    • 1970-01-01
    • 2017-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多