wbc*_*son 0 c terminal xcode pthreads segmentation-fault
我在pthread内的以下代码块中得到"Segmentation Fault 11":
void *func(void *len){
char *temp = len;
size = (int) strtol(temp, (char **)NULL, 10); // this throws the segfault
}
Run Code Online (Sandbox Code Playgroud)
pthread创建如下:
int main(int argc, const char *argv[]){
pthread_t t0;
const char * length = argv[1];
pthread_create(&t0, NULL, &func, (void *)length);
// rest of code
...
}
Run Code Online (Sandbox Code Playgroud)
令我困惑的是SegFault在编译并在终端中运行时被抛出但在Xcode中没有.有没有想过它被抛出的原因?
小智 5
除非你等待线程,否则可能就是main()在func()执行之前退出(因此argv它会失效,因为它超出了范围).我的猜测是,这是一种竞争条件,在调试器内部运行时会被消除,这就是为什么在"Xcode"中运行它(重要的不是IDE,而是正在调试该过程的事实).
| 归档时间: |
|
| 查看次数: |
626 次 |
| 最近记录: |