【发布时间】:2019-01-22 15:04:37
【问题描述】:
我不知道我错在哪里,在运行代码到达它运行 pthread_join() 的位置后,许多 pthread_join() 返回值 3 而不是 0。此外,打印 @987654324 的值@ 并不总是一致的,这会导致分段错误并在同一位置打印多次。
在 cmets 中按要求修改代码
所有包含的内容都适用于程序的其他部分。仅测试这段代码会在 pthread_join() 上的错误 3 处产生分段错误
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <signal.h>
#include <pthread.h>
#include <errno.h>
#include <config.h>
#include <sys/select.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
void *threadF(){
printf("hello\n");
pthread_exit((void*)0);
}
int main(int argc, char *argv[]) {
FILE *fileconf=fopen(argv[2],"r");
if(fileconf==NULL){
fprintf(stderr, "Fopen\n",argv[2]);
return -1;
}
set_conf(fileconf); //parse fileconf and set THREADSINPOOL correctly
pthread_t array[THREADSINPOOL];
int i,err,s=0;
for(i=0;i<THREADSINPOOL;i++){
if((err=pthread_create(&array[i],NULL,&threadF,NULL))!=0){
fprintf(stderr,"thread\n");
exit(errno);
}
}
int tmp;
for(i=0;i<THREADSINPOOL;i++){
tmp=pthread_join(array[i],(void *)&s);
printf("thread: %lu terminated\n tmp: %d\n",array[i],tmp);
}
return 0;
}
【问题讨论】:
-
需要minimal reproducible example。您描述的症状表明某种未定义的行为。如果没有完整的代码集(我的意思是一组可以编译和运行的代码,但要尽可能短),我们将无法看到这一点。
-
首先要了解
3的含义。这就是man页面的作用所在。 -
代码已编辑,这个简单的代码会导致段错误
-
您的
printf()格式需要两个值,但您只传递了 1,这会导致未定义的行为并且所有赌注都关闭...如果您的编译器没有警告您,请记住始终在 gcc 和 clang 上至少使用-Wall -Wextra进行编译。 -
pthread_join正在使用void **而不是void *。显然int s可能没有空间容纳void*。
标签: c multithreading pthreads threadpool pthread-join