简体   繁体   English

OSX上的总线错误-pthreads

[英]Bus error on OSX - pthreads

am trying to get my head around the following: 我试图使我的头脑以下:

Have a small program am trying to port to OSX(intel) which calls function doWork() via pthread_create, in the function, I start by creating an array of long like such: 有一个小程序正在尝试移植到OSX(intel),后者通过pthread_create调用函数doWork(),在该函数中,我首先创建一个如下所示的long数组:

long myarray[DIMENSION]

on OSX, for the following values of DIMENSION, I get the following: 在OSX上,对于以下DIMENSION值,我得到以下信息:

0->65434 = fine
65435->67037 = SIGBUS
67037+ = SIGSEGV

I'm totally confused here, I understand that SIGBUS is due to memory alignment issues usually, I checked sizeof(long) and it appears to be 8 on this platform. 我在这里完全感到困惑,我知道SIGBUS通常是由于内存对齐问题引起的,我检查了sizeof(long)并在此平台上看起来是8。 Can somebody point me in the right direction of docs I should be reading here? 有人可以指出我应该在这里阅读的文档的正确方向吗?

Here is the source: 来源如下:


#include pthread.h
#include stdio.h
#define NUM_THREADS     5
#define DIMENSION       12345

void *doStuff(void *threadid)
{
   long array[DIMENSION];
   pthread_exit(NULL);
}

int main (int argc, char *argv[])
{
   pthread_t threads[NUM_THREADS];
   int rc;
   long t;
   for(t=0; t lt NUM_THREADS; t++){
      printf("In main: creating thread %ld\n", t);
      rc = pthread_create(&threads[t], NULL, doStuff, (void *)t);
      if (rc){
         printf("ERROR; return code from pthread_create() is %d\n", rc);
         exit(-1);
      }
   }
   pthread_exit(NULL);
}

It would appear you're overflowing the stack. 看来您正在溢出堆栈。

You'll need to either turn the long array into a malloced one, or use pthread_attr_setstacksize and friends to create a larger stack when you call pthread_create. 您需要将长数组转换为malloc数组,或者在调用pthread_create时使用pthread_attr_setstacksize和朋友创建更大的堆栈。

Default thread stack sizes vary a lot before platforms, which would explain why the code works on other platforms. 平台之前的默认线程堆栈大小相差很大,这可以解释为什么代码可以在其他平台上运行。

https://computing.llnl.gov/tutorials/pthreads/#Stack has some example code. https://computing.llnl.gov/tutorials/pthreads/#Stack提供了一些示例代码。

As to why you get a sigbus, it's probably because the act of creating the array is overwriting some part of pthreads internal data structures with garbage, resulting in an alignment error when pthreads tries to clean up the thread. 至于为什么要获得sigbus,可能是因为创建数组的行为是用垃圾覆盖pthreads内部数据结构的某些部分,从而在pthreads尝试清理线程时导致对齐错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM