pthread创建和退出

来源:互联网 发布:局域网域名 编辑:程序博客网 时间:2024/06/05 23:02
#include <pthread.h>#include <stdio.h>#define NUM_THREAD 5void *print_hello(void *threadid){        long tid;        tid = (long)threadid;        printf("Hello world! thread#%ld\n", tid);        pthread_exit(NULL);}int main(){        pthread_t threads[NUM_THREAD];        int rc;         long i;        for(i =0; i<NUM_THREAD; i++)        {                   printf("In main: create thread %ld\n", i);                 rc = pthread_create(&threads[i], NULL, print_hello, (void*)i);                if(rc)                {                           printf("ERROR: return code from pthread_create() is %d", rc);                        exit(1);                }           }           pthread_exit(NULL);        return 0;}
输出:
In main: create thread 0In main: create thread 1In main: create thread 2In main: create thread 3In main: create thread 4Hello world! thread#4Hello world! thread#3Hello world! thread#2Hello world! thread#1Hello world! thread#0
0 0
原创粉丝点击