2016-09-20 14 views
1

このコードセグメントでは、関数pthread_join()を使用せずにこれらのスレッドを作成するにはどうすればよいですか? pthread_exit()を使用しても機能しませんでした。 pthread_join()でコンパイルCで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> 
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 

+0

スレッドを作成し、pthread_joinを呼び出さないだけで、pthread_joinを呼び出さずにスレッドを作成できます。本当の質問は何ですか? – immibis

+0

@immibisこの特定のケースでは、スレッドは実行を終了しません。つまり、最後まで実行されません。しかし、pthread_join()はそれを実行します。 – sh1ftz

+0

はい、この特定のケースでは、スレッドが終了する前にプログラムが終了するためです。 – immibis

答えて

1

pthread_join()関数を使用せずにこれらのスレッドを作成するにはどうすればよいですか?

pthread_join()を呼び出すことが、プロセスを終了し、これは彼らが便利な何かをする前に開始した可能性が高い、そのすべてのスレッドを終了しpthread_create()と呼ばれた後main()を残していません。

プロセスを維持したままで、main()のままにするには、returnの代わりにpthread_exit()を呼び出してください。

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

    pthread_exit(NULL); 
} 
2

あなたは出口にスレッドを待つためにpthread_joinを使用して、スレッドを作成するためにpthread_joinを使用しないでください。

スレッドはプロセスのサブユニットです。スレッドは、同じプロセス内の他のスレッドと同じメモリ空間で実行されます。その結果、プロセスが終了すると、それに属する実行中のスレッドはすべて終了し、メインを終了すると(exitまたはreturnによって)本質的にプロセスが終了します。

pthreadsにはスレッドをプロセスから切り離すためのapiがありません。

それはあなたがやろうとしているものだ場合は、プロセスを作成する必要があります、そしてposix_spawnまたはfork/execのいずれかになります。

あなたは、単に明示的に終了させるためにそれぞれにそれらを待つ必要がスレッドを作成しないようにしようとしている場合は、pthread_exitを呼び出すことができます。http://man7.org/linux/man-pages/man3/pthread_exit.3.html

他のスレッドが実行を継続できるようにするには、メインスレッドが終了する必要がありますexit(3)ではなく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> 

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

0

One alternative methodそれが進行する前に終了し、すべてのスレッドを待つために、あなたのプログラムを強制するためのpthread_barrier_wait()を呼び出すことです。しかし、なぜpthread_join()を使用できないのですか?あなたはスレッドを生かし続ける必要がありますか?

関連する問題