线程返回值传出

#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>

void printId(const char* s)
{
    pid_t pid = getpid();
    pthread_t tid = pthread_self();
    printf("%s pid %u tid %u (0x%x)\n",s,(unsigned int)pid,(unsigned int)tid,(unsigned int)tid);
}

void* testThread(void* /*p*/)
{
    printId("new  thread:");
    //将返回值传出
    pthread_exit((void*)"thread finished");
    return 0;
}

int main()
{
    pthread_t pid;
    pthread_create(&pid,NULL,testThread,NULL);
    void* ret = NULL;
    pthread_join(pid,&ret);
    printId("main thread:");
    printf("ret:%s",(char*)ret);
}

new  thread: pid 20959 tid 3077462896 (0xb76e5b70)
main thread: pid 20959 tid 3077469904 (0xb76e76d0)
ret:thread finished

猜你喜欢

转载自xiangjie88.iteye.com/blog/2111200