在这段代码中,如何在不使用函数pthread_join()的情况下创建这些线程?使用pthread_exit()不起作用。
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
typedef struct{
char name[100];
char search[100];
pthread_t tid;
} mystruct;
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
void* sfun(void *arg){
mystruct *sfile= (mystruct *) arg;
int fd=open(sfile->name,O_RDONLY);
if(fd<0){
printf("Error%s\n", sfile->name);
pthread_exit(NULL);
}
int n=1;
pthread_mutex_lock(&mutex);
while(n!=0){
n=match_line(fd,sfile->search);
printf("%s:\t%d\n",sfile->name,n);
}
pthread_mutex_unlock(&mutex);
close(fd);
return NULL;
}
int main(int argc, char *argv[]){
if(argc< 3){
printf("Error\n");
exit(1);
}
mystruct file[10];//max 10 threads
int c;
for(c=0;c<argc-2;c++){
strcpy(file[c].search,argv[1]);
strcpy(file[c].name,argv[c+2]);
pthread_create(&file[c].tid,NULL,sfun,&file[c]);
}
for(c=0;c<argc-2;c++)
pthread_join(file[c].tid,NULL);
return 0;
}使用pthread_join()编译:
./ppc "Sherlock" testfile1 testfile2 testfile12
testfile1: 5
testfile1: 762
testfile1: 960
testfile1: 977
testfile1: 1025
testfile1: 1034
testfile1: 1049
testfile1: 1068
testfile1: 1080
testfile1: 1123
testfile1: 0
testfile2: 3
testfile2: 90
testfile2: 170
testfile2: 179
testfile2: 473
testfile2: 643
testfile2: 760
testfile2: 811
testfile2: 836
testfile2: 978
testfile2: 0
testfile12: 5
testfile12: 762
testfile12: 960
testfile12: 977
testfile12: 1025
testfile12: 1034
testfile12: 1049
testfile12: 1068
testfile12: 1080
testfile12: 1123
testfile12: 1129
testfile12: 1216
testfile12: 1296
testfile12: 1305
testfile12: 1599
testfile12: 1769
testfile12: 1886
testfile12: 1937
testfile12: 1962
testfile12: 2104
testfile12: 0如果没有pthread_join(),它将不会打印任何东西!
发布于 2016-09-20 14:05:02
不使用pthread_join来创建线程,而是使用pthread_join来等待线程退出。
线程是进程的子单元:它们与同一进程中的其他线程运行在相同的内存空间中。因此,当进程结束时,属于它的所有正在运行的线程都会终止,退出main (通过exit或return)实质上会结束该进程。
pthreads没有用于将线程从其进程中分离出来的api。
如果这就是您想要做的,那么您将不得不创建进程,并且应该查看posix_spawn或fork/exec。
如果您只是尝试创建线程,而不必显式地等待每个线程终止,则可以调用pthread_exit:http://man7.org/linux/man-pages/man3/pthread_exit.3.html
退出为了允许其他线程继续执行,主线程应该通过调用pthread_exit()而不是
(3)来终止。
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
void* tfun(void*)
{
sleep(3);
printf("Ni!\n");
return NULL;
}
int main()
{
pthread_t t;
pthread_create(&t,NULL,&tfun,NULL);
int ret = 0;
pthread_exit(&ret);
}现场演示:http://coliru.stacked-crooked.com/a/b47d3682008aed05
发布于 2016-09-20 13:56:37
如何在不使用pthread_join()函数的情况下创建这些线程?
不调用pthread_join()而是在调用pthread_create()之后离开main()会退出进程,并随之终止所有线程,很可能是在它们开始做任何有用的事情之前。
要使进程保持活动状态并仍然离开main(),可以通过调用pthread_exit()而不是执行return来实现。
int main(int argc, char *argv[])
{
...
pthread_exit(NULL);
}发布于 2016-09-21 12:32:49
强制您的程序在继续之前等待所有线程完成的One alternative method是调用pthread_barrier_wait()。但是为什么你不能使用pthread_join()呢?你需要让线程保持活动状态吗?
https://stackoverflow.com/questions/39584027
复制相似问题