在这段代码中,我试图创建一个线程数组。我已将pthreads值设置为1。在for循环中,将打印地址,ID和“创建的”,但是该函数似乎从未执行,因为我没有得到打印。
seg错误实际上发生在底部的pthread_join处,但是肯定是由于创建导致的,因为该函数永远不会运行,对吗?自我刚刚测试1以来,producers [0]是唯一的线程。create函数像预期的那样传递(ID地址,NULL,void * function()和void *参数)。它一定很明显,但是我一直想弄清楚我很久没空。
typedef struct ThreadStruct {
int threadIndex;
} ThreadStruct;
void *producer(void* ts){
printf("in producer\n");
FILE* inputfp = threadstruct->inputFiles[0];
hostname = (char*)malloc((MAXNAME+1)*sizeof(char));
while(fscanf(inputfp, hostname) > 0){
printf("%s",hostname);
}
return 0;
}
int main(int argc, char *argv[]){
int pthreads = atoi(argv[1]); //always 1
ThreadStruct threadstruct;
pthread_t producers[pthreads];
for(int i=0;i<pthreads;i++){
printf("in pthread loop\n");
printf("%p \n",&producers[i]);
printf("%ld \n",producers[i]);
pthread_create(&producers[i], NULL, producer, (void *) &threadstruct);
}
pthread_join(producers[0], NULL);
}
答案 0 :(得分:0)
您似乎并没有检查是否实际创建了线程,换句话说,您似乎没有验证pthread_create
的返回类型和直接调用{{ 1}}可能导致未定义的行为,以防pthread_join
失败。
pthread_create
pthread_create(&producers[i], NULL, producer, (void *) &threadstruct);
但是在某些情况下, pthread_join with invalid thread handle seg faults。
上面链接的一部分摘录
“ *将无效句柄传递给pthread_join就像将无效指针传递给系统调用一样。
该实现可以检测到它并在errno中发信号,但可能会崩溃 好吧。
在Red Hat Linux上有2种不同的POSIX线程实现
您可以使用LD_ASSUME_KERNEL = 2.4.19请求老式线程库 在环境中。
对于NPTL,其中pthread_t是指针,pthread_join检测到一些无效 处理可以廉价完成的地方。但是检测所有可能的无效 句柄将非常昂贵(实现基本上必须采取 全局锁并遍历当前正在运行的所有线程,比较 与他们每个人。通过说100000个运行线程,您可以看到 这是很昂贵的。)
此测试用例在SuSE上有效,因为SuSE仅运送老式的不可缩放 linuxthreads实现。*“
The pthread_join() function shall fail if:
EINVAL The implementation has detected that the value specified by thread does not refer to a joinable thread.
ESRCH No thread could be found corresponding to that specified by the given thread ID.
也许是这样的
RETURN VALUE
On success, pthread_create() returns 0; on error, it returns an error number, and the contents of *thread are undefined.
答案 1 :(得分:0)
您的代码在我这边工作,不会产生seg错误。 这是打印输出信息:
$ ./pthread
in pthread loop
0xffffcb70
6444554784
in pthread loop
in producer
0xffffcb78
64
in pthread loop
in producer
0xffffcb80
1
in pthread loop
in producer
0xffffcb88
4299166064
in pthread loop
in producer
0xffffcb90
6443761969
in pthread loop
in producer
0xffffcb98
6445451520
in pthread loop
in producer
0xffffcba0
314761761601
in producer
exit main thread
您的代码在这里:
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
typedef struct ThreadStruct {
int threadIndex;
} ThreadStruct;
void *producer(void* ts){
printf("in producer\n");
return 0;
}
int main(int argc, char *argv[]){
int pthreads = 7;
ThreadStruct threadstruct;
pthread_t producers[pthreads];
for(int i=0;i<pthreads;i++){
printf("in pthread loop\n");
printf("%p \n",&producers[i]);
printf("%ld \n",producers[i]);
pthread_create(&producers[i], NULL, producer, (void *) &threadstruct);
}
pthread_join(producers[0], NULL);
printf("exit main thread\n");
}
答案 2 :(得分:0)
首先检查pthread_create()
的返回值以检查其是否成功。对于例如
int ret = pthread_create(&producers[i], NULL, producer, (void *) &threadstruct);
if(ret) {
fprintf(stderr, "failed to create thread %s\n",strerror(ret));
exit(EXIT_FAILURE);
}
另外,建议对所有线程都使用pthread_join()
,不仅要对第一个线程使用,就好像您要等待线程终止一样,再在here中对主线程进行进一步处理。还要检查pthread_join()
的返回值。
示例代码
#include<stdio.h>
#include<string.h>
#include<stdint.h>
#include<stdlib.h>
#include<pthread.h>
typedef struct ThreadStruct {
int threadIndex;
} ThreadStruct;
void *producer(void* ts){
printf("in producer\n");
return 0;
}
int main(int argc, char *argv[]){
int pthreads = 5;
int ret;
ThreadStruct threadstruct;
pthread_t producers[pthreads];
/* create threads */
for(intptr_t itr = 0;itr < pthreads; itr++){
printf("in pthread loop\n");
//printf("%p \n",&producers[itr]);
//printf("%ld \n",producers[itr]);
ret = pthread_create(&producers[itr], NULL, producer, (void *) &threadstruct);
if (ret) {
fprintf(stderr, "failed to create thread #%ld - %s\n",(long)itr, strerror(ret));
exit(EXIT_FAILURE);
}
}
/* join all threads */
for(intptr_t itr = 0; itr < pthreads; itr++){
printf("In main: joining thread #%ld\n", (long)itr);
ret = pthread_join(producers[itr], NULL);
if (ret) {
fprintf(stderr, "failed to join thread #%ld - %s\n",(long)itr, strerror(ret));
exit(EXIT_FAILURE);
}
}
//pthread_exit();
return 0;
}
它在我的系统中有效,希望它也对您有用。