编译pthreads程序时出现问题

Waz*_*ery 0 c gcc pthreads

我尝试用这个命令编译这个简单的pthreads程序

$ gcc -pthread -o pthreads pthreads.c
Run Code Online (Sandbox Code Playgroud)
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

void *myThread(void *arg);

int main()
{
    pthread_t mythread;
    int ret;

    ret = pthread_create( &mythread, NULL, myThread, NULL );

    if (ret != 0){
        printf( "Can't create pthread: %s", strerror(errno));
        exit(-1);
    }
    return 0;
}

void *myThread(void *arg){

    // Thread code goes here..
    printf("OK! NOW ON THE THREAD\n");
    pthread_exit(NULL);
}
Run Code Online (Sandbox Code Playgroud)

但是在尝试./pthreads时没有输出!

San*_*uja 6

你需要等待线程完成.否则,您可能会在线程开始执行之前退出.

... 
pthread_create( &mythread, NULL, myThread, NULL );
...
// Wait for the thread to finish.
pthread_join( mythread, NULL);
Run Code Online (Sandbox Code Playgroud)