C pthread连接/退出线程(退出无效)
我试图学习线程,但我有一个问题。。 我正在尝试创建由ThreadCount定义的多个线程,并在工作完成后将它们全部关闭C pthread连接/退出线程(退出无效),c,linux,multithreading,pthreads,C,Linux,Multithreading,Pthreads,我试图学习线程,但我有一个问题。。 我正在尝试创建由ThreadCount定义的多个线程,并在工作完成后将它们全部关闭 #define ThreadCount 5 只是我的主要观点的一小段 pthread_t pro; pthread_t con[ThreadCount]; pthread_attr_t attr; pthread_attr_init(&attr); pthread_create(&pro, &attr, producer, fifo); for(un
#define ThreadCount 5
只是我的主要观点的一小段
pthread_t pro;
pthread_t con[ThreadCount];
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(&pro, &attr, producer, fifo);
for(unsigned int i = 0; i <ThreadCount;++i){
pthread_create (&con[i], NULL, consumer, createhilfsStruct(i,fifo));
}
for(unsigned int i = 0; i <ThreadCount;++i){
pthread_join (con[i], NULL);
}
pthread_join (pro, NULL);
printf("Done");
pthread_exit(NULL);
在我的消费者函数中,我有一个查询来检查fifo->done是否为真,如果为真,我使用exit(0);要终止程序==>printf(“完成”)在主程序中无法调用[显然]
如果我将出口(0)交换为pthread_出口(3);//我在网上找到了这个,但我不明白为什么
它不会退出线程,程序只是处于空闲状态,从不关闭/从未到达pthread_join以下的任何位置
我的第一次尝试是使用pthread\u t con,pro
而不是pthread\u t con[…]
,只是pthread\u join(con,NULL)
,但这也不起作用
我做错了什么
各位,祝你们今天愉快你们的帖子到底在做什么?也许您可以尝试简化它们,直到它们工作,然后一步一步地添加一个小东西,直到它停止工作,然后以这种方式找到有问题的代码?您是否有死锁或数据竞争?您是否尝试过使用诸如(更具体地说是its)之类的工具?hello@Someprogrammerdude ty是您的答案!到目前为止我发布的所有内容都正确吗?这是我的main.c | | |制作人正在阅读一个带有链接的文件,消费者正在下载URL | | | | askServer(const char*地址,const char*页面,const char*文件名)| | |如果你想下载www.youtube.com/new,你会像askServer一样传递它('www.youtube.com','/new',你想把它安全地传递给它)顺便说一句,如果我在所有作业完成后使用exit(0)终止它,程序将“正确”完成,但是如果我尝试使用pthread_exit(3),程序将永远不会停止,所以我猜这不可能是死锁/数据竞争,对吧?我不知道如何使用valgrind,但我用gcc-Wall-Werror-g-pthread-std=c99 main.c socket.o-o test | |然后运行valgrind | | valgrind./test |,输出与以前一样//否(valgrind)发生错误完成作业后它仍然处于空闲状态七如果我尝试将pthread_exit(0)与printf(“test”)交换,它不会输出任何内容,只是exit(0)到目前为止工作正常,但我只想杀死线程而不是整个程序。你的线程实际上在做什么?也许您可以尝试简化它们,直到它们工作,然后一步一步地添加一个小东西,直到它停止工作,然后以这种方式找到有问题的代码?您是否有死锁或数据竞争?您是否尝试过使用诸如(更具体地说是its)之类的工具?hello@Someprogrammerdude ty是您的答案!到目前为止我发布的所有内容都正确吗?这是我的main.c | | |制作人正在阅读一个带有链接的文件,消费者正在下载URL | | | | askServer(const char*地址,const char*页面,const char*文件名)| | |如果你想下载www.youtube.com/new,你会像askServer一样传递它('www.youtube.com','/new',你想把它安全地传递给它)顺便说一句,如果我在所有作业完成后使用exit(0)终止它,程序将“正确”完成,但是如果我尝试使用pthread_exit(3),程序将永远不会停止,所以我猜这不可能是死锁/数据竞争,对吧?我不知道如何使用valgrind,但我用gcc-Wall-Werror-g-pthread-std=c99 main.c socket.o-o test | |然后运行valgrind | | valgrind./test |,输出与以前一样//否(valgrind)发生错误完成作业后它仍然处于空闲状态七如果我尝试将pthread_exit(0)与printf(“test”)交换,它不会输出任何内容,只是exit(0)到目前为止工作正常,但我只想杀死线程,而不是整个程序
typedef struct{
int threadName;
queue* queue;
}hilfsStruct;
typedef struct {
job* buf[QUEUESIZE];
long head, tail;
int full, empty, currentPages, done;
pthread_mutex_t *mut;
pthread_cond_t *notFull, *notEmpty;
} queue;
hilfsStruct* createhilfsStruct(int threadName, queue* q){
hilfsStruct* h = (hilfsStruct*)malloc (sizeof (hilfsStruct));
h->queue = q;
h->threadName = threadName;
return h;
}
queue *queueInit (void) {
queue *q;
q = (queue *)malloc (sizeof (queue));
if (!q) return (NULL);
q->empty = 1;
q->full = 0;
q->head = 0;
q->tail = 0;
q->currentPages = 0;
q->done = 0;
q->mut = (pthread_mutex_t *) malloc (sizeof (pthread_mutex_t));
pthread_mutex_init (q->mut, NULL);
q->notFull = (pthread_cond_t *) malloc (sizeof (pthread_cond_t));
pthread_cond_init (q->notFull, NULL);
q->notEmpty = (pthread_cond_t *) malloc (sizeof (pthread_cond_t));
pthread_cond_init (q->notEmpty, NULL);
return (q);
}