我在pthread内的以下代码块上得到一个“Segmentation Fault 11”:
void *func(void *len){
char *temp = len;
size = (int) strtol(temp, (char **)NULL, 10); // this throws the segfault
}
pthread create如下所示:
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
...
}
让我困惑的是,SegFault是在终端中编译和运行时抛出的,而不是在Xcode中。知道为什么要扔吗?
最佳答案
除非您等待线程,否则可能是在main()
之前的func()
退出被执行(因此argv
无效,因为它超出了范围)。我的猜测是,这是在调试器中运行时消除的竞争条件,这就是为什么“在Xcode中”运行它(重要的不是IDE,而是进程正在被调试)有帮助。
关于c - 在带有pthread函数的strtol上的段错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16023422/