【问题标题】:pthread_join always causes SIGSEGVpthread_join 总是导致 SIGSEGV
【发布时间】:2016-06-06 03:46:11
【问题描述】:

由于某种原因,当我使用 Valgrind 运行时,pthread_join 总是会在我的计算机上引发 SIGSEGV 操作。为了测试这一点,我从https://computing.llnl.gov/tutorials/pthreads/ 运行了以下代码:

/* pthread.c */

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define NUM_THREADS 4

void *BusyWork(void *t)
{
  int i;
  long tid;
  double result=0.0;
  tid = (long)t;
  printf("Thread %ld starting...\n",tid);
  for (i=0; i<1000000; i++)
  {
    result = result + sin(i) * tan(i);

  }
  printf("Thread %ld done. Result = %e\n",tid, result);
  pthread_exit((void*) t);

}

int main (int argc, char *argv[])
{
  pthread_t thread[NUM_THREADS];
  pthread_attr_t attr;
  int rc;
  long t;
  void *status;

  /* Initialize and set thread detached attribute */
  pthread_attr_init(&attr);
  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

  for(t=0; t<NUM_THREADS; t++) {
    printf("Main: creating thread %ld\n", t);
    rc = pthread_create(&thread[t], &attr, BusyWork, (void *)t); 
    if (rc) {
      printf("ERROR; return code from pthread_create() is %d\n", rc);
      exit(-1);

    }

  }

  /* Free attribute and wait for the other threads */
  pthread_attr_destroy(&attr);
  for(t=0; t<NUM_THREADS; t++) {
    rc = pthread_join(thread[t], &status);
    if (rc) {
      printf("ERROR; return code from pthread_join() is %d\n", rc);
      exit(-1);

    }
    printf("Main: completed join with thread %ld having a status   of %ld\n",
        t,(long)status);

  }

  printf("Main: program completed. Exiting.\n");
  pthread_exit(NULL);
}

跑步

$ clang -g -o pthread pthread.c
$ valgrind --leak-check=full ./pthread

产生以下 valgrind 错误:

==17283== Process terminating with default action of signal 11 (SIGSEGV)
==17283==  Access not within mapped region at address 0x700002F3CC3E
==17283==    at 0x10044437F: _pthread_find_thread (in /usr/lib/system/libsystem_pthread.dylib)
==17283==    by 0x100446D52: _pthread_join_cleanup (in /usr/lib/system/libsystem_pthread.dylib)
==17283==    by 0x100446C63: pthread_join (in /usr/lib/system/libsystem_pthread.dylib)
==17283==    by 0x100000D52: main (pthread.c:50)

我的电脑是 Mac OS X (El Capitan)。对于 clang,我使用的是 Apple LLVM 版本 7.0.2 (clang-700.1.81)。对于 Valgrind,我使用的是 valgrind-3.11.0。

【问题讨论】:

  • 我在使用简单的 C++ 时遇到了完全相同的问题:stackoverflow.com/questions/36576251/…
  • 根据 KevinD 的链接,有一个官方的 bug report 用于此,但它可能是带有 libpthread 的 issue。它似乎也导致 Mac 上 leak-check 模式下的断言失败,回复:Philippe W.stated "在 linux 上没有复制器,没有太多机会 [原文如此] 找到邮件的问题远程调试”

标签: c pthreads pthread-join


【解决方案1】:

我无法在内核 14.5.0 的 Mac OS X Yosemite (10.10.5) 上重现您的问题。我使用的 Clang 和 Valgrind 版本与您的相同(请仔细检查以防万一):

$ clang --version
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin14.5.0
Thread model: posix

$ valgrind --version
valgrind-3.11.0

因此,唯一的区别是操作系统版本。以下是我遵循的步骤:

  1. 通过运行刷新了开发者工具的安装 xcode-select --install。如果不这样做,我就无法建造 瓦尔格林。
  2. CCCXX 环境变量设置为clangand clang++,分别。我这样做是为了确保 Clang,而不是 GCC, 用来。设置 CXX 可能是矫枉过正。后来我清除了 Valgrind 安装并重新安装 与 GCC,但它仍然是成功的,即使我认为我是 收到更多警告。
  3. ./configure --prefix=/usr/local 在未解压的根目录中 Valgrind tarball,后跟 makesudo make install

希望这些步骤可以帮助您解决问题。

【讨论】:

  • 感谢您的建议。我用你要求的信息更新了我的帖子。当我在没有 valgrind 的情况下运行程序时,我没有问题。
猜你喜欢
  • 2014-04-26
  • 1970-01-01
  • 1970-01-01
  • 2017-11-09
  • 1970-01-01
  • 2019-07-21
  • 2010-12-06
  • 1970-01-01
相关资源
最近更新 更多