在C中创建没有pthread_连接的线程

在C中创建没有pthread_连接的线程,c,multithreading,pthreads,pthread-join,C,Multithreading,Pthreads,Pthread Join,在这段代码中,如果不使用函数pthread\u join(),如何创建这些线程?使用pthread\u exit()无效 #include <stdio.h> #include <pthread.h> #include <string.h> #include <stdlib.h> #include <sys/types.h> #include <fcntl.h> #include <unistd.h> #incl

在这段代码中,如果不使用函数
pthread\u join()
,如何创建这些线程?使用
pthread\u 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\u join()
它不会打印任何内容

如果不使用函数pthread_join(),如何创建这些线程

不调用
pthread\u join()
,但在调用
pthread\u create()
后离开
main()
将退出进程,并由此终止其所有线程,很可能是在它们开始做任何有用的事情之前

要使进程保持活动状态并仍然离开
main()
,请调用
pthread\u exit()
,而不是执行
返回操作

int main(int argc, char *argv[])
{
  ...

  pthread_exit(NULL);
}
如果不使用函数pthread_join(),如何创建这些线程

不调用
pthread\u join()
,但在调用
pthread\u create()
后离开
main()
将退出进程,并由此终止其所有线程,很可能是在它们开始做任何有用的事情之前

要使进程保持活动状态并仍然离开
main()
,请调用
pthread\u exit()
,而不是执行
返回操作

int main(int argc, char *argv[])
{
  ...

  pthread_exit(NULL);
}

您没有使用
pthread\u join
创建线程,而是使用
pthread\u join
等待线程退出

线程是进程的子单元:它们与同一进程中的其他线程在相同的内存空间中运行。因此,当进程结束时,属于它的任何正在运行的线程都将终止,退出main(通过exit或return)本质上结束进程

pthreads没有用于将线程与其进程分离的api

如果您正试图这样做,则必须创建进程,并且应该查看
posix_spawn
fork
/
exec

如果您只是尝试创建线程,而不必显式地等待线程终止,则可以调用
pthread\u exit

为了允许其他线程继续执行,主线程应该通过调用pthread_exit()而不是exit(3)来终止

#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
void*tfun(void*)
{
睡眠(3);
printf(“Ni!\n”);
返回NULL;
}
int main()
{
pthread_t;
pthread_create(&t,NULL,&tfun,NULL);
int-ret=0;
pthread_退出(&ret);
}

实时演示:

您不使用
pthread\u join
创建线程,而是使用
pthread\u join
等待线程退出

线程是进程的子单元:它们与同一进程中的其他线程在相同的内存空间中运行。因此,当进程结束时,属于它的任何正在运行的线程都将终止,退出main(通过exit或return)本质上结束进程

pthreads没有用于将线程与其进程分离的api

如果您正试图这样做,则必须创建进程,并且应该查看
posix_spawn
fork
/
exec

如果您只是尝试创建线程,而不必显式地等待线程终止,则可以调用
pthread\u exit

为了允许其他线程继续执行,主线程应该通过调用pthread_exit()而不是exit(3)来终止

#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
void*tfun(void*)
{
睡眠(3);
printf(“Ni!\n”);
返回NULL;
}
int main()
{
pthread_t;
pthread_create(&t,NULL,&tfun,NULL);
int-ret=0;
pthread_退出(&ret);
}

实时演示:

要强制程序在继续之前等待所有线程完成,请调用
pthread\u barrier\u wait()
。但是为什么不能使用
pthread\u join()
?是否需要使线程保持活动状态?

要强制程序等待所有线程完成后再继续,请调用
pthread\u barrier\u wait()
。但是为什么不能使用
pthread\u join()
?是否需要保持线程的活动状态?

只需创建线程而不调用pthread\u join,就可以创建线程而不调用pthread\u join。你真正的问题是什么?@immibis在这种特殊情况下,线程不会完成执行,我的意思是它不会运行到最后。但是使用pthread_join()它会运行。是的,因为在这种特殊情况下,程序会在线程完成之前退出。是的,那么,如果不使用pthread_join()@immibis,我如何避免这种情况呢?那么,在退出之前,您必须有一些方法等待线程完成。pthread_join是一种明显的方式,或者您可以使用条件变量和互斥来做同样的事情,或者让线程在完成时写入管道并从中读取主线程,或者您可以告诉用户“线程完成时请按enter键”,然后等待他们按enter键。但是pthread_-join有什么错呢?只需创建线程而不调用pthread_-join,就可以创建线程而不调用pthread_-join。你真正的问题是什么?@immibis在这种特殊情况下,线程不会完成执行,我的意思是它不会运行到最后。但是使用pthread_join()它会运行。是的,因为在这种特殊情况下,程序会在线程完成之前退出。是的,那么,如果不使用pthread_join()@immibis,我如何避免这种情况呢?那么,在退出之前,您必须有一些方法等待线程完成。pthread_join是一种明显的方式,或者您可以使用条件变量和互斥来做同样的事情,或者让线程在完成时写入管道并从中读取主线程,或者您可以告诉用户“线程完成时请按enter键”,然后等待他们按enter键。但是pthread_join有什么问题?
#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);
}