多线程代码实例

来源:互联网 发布:wow采集助手数据库 编辑:程序博客网 时间:2024/05/24 23:12

例1 多线程及参数传递

#include<iostream>#include<cstdlib>#include<pthread.h>using namespace std;#define NUM_THREADS 5void *say_hello1(void *args){    int i = *( (int *)args );  //对传入的参数进行强制类型转换,由无类型指针转变为整形指针,再用*读取其指向到内容    cout << "hello in " << i << endl;} //函数返回的是函数指针,便于后面作为参数/********************************************************************/void *say_hello2(void *args){    int i = (long)args;  //对传入的参数进行强制类型转换,由无类型指针转变为整形指针,再用*读取其指向到内容    cout << "hello in " << i << endl;} //函数返回的是函数指针,便于后面作为参数/*****************************************************************************/int main(){    pthread_t tids[NUM_THREADS];    cout << "hello in main..." << endl;    for(int i = 0 ; i < NUM_THREADS; ++ i){        //int ret = pthread_create( &tids[i], NULL, say_hello1, (void*)&i);//传入到参数必须强转为void*类型,即无类型指针,&i表示取i的地址,即指向i的指针        int ret = pthread_create( &tids[i], NULL, say_hello2, (void*)i);        cout << "Current pthread id = " << tids[i] << endl;        if(ret != 0){            cout << "pthread_create error: error_code = " << ret << endl;        }    }    pthread_exit(NULL);}
  • 调用void *say_hello1(void *args)输出结果:
hello in main...Current pthread id = 139639619233536Current pthread id = 139639610840832Current pthread id = 139639602448128hello in hello in 3hello in 33hello in 3Current pthread id = 139639594055424Current pthread id = 139639585662720hello in 5
  • 调用void *say_hello2(void *args)输出结果:
hello in main...Current pthread id = 139654340855552hello in 0Current pthread id = 139654332462848hello in 1Current pthread id = 139654324070144hello in 2Current pthread id = 139654315677440hello in 3Current pthread id = 139654307284736hello in 4

例2 结构体传递多个参数

#include<iostream>#include<cstdlib>#include<pthread.h>using namespace std;#define NUM_THREADS 5struct  thread_data{    int thread_id;    char *message;};void *PrintHello(void *threadarg){    struct thread_data *may_data;    may_data = (struct thread_data *) threadarg;    cout << "thread ID: " << may_data->thread_id;    cout << "message: " << may_data->message << endl;    pthread_exit(NULL);}int main(){    pthread_t threads[NUM_THREADS];    struct thread_data td[NUM_THREADS];    int rc;    int i;    for(i = 0; i < NUM_THREADS; ++ i){        cout << "main(): creating thread, " << i << endl;        td[i].thread_id = i;        td[i].message = "this is message";        rc = pthread_create(&threads[i], NULL, PrintHello, (void*)&td[i]);        if(rc){            cout << "error" << rc << endl;        }    }    pthread_exit(NULL);}

输出结果:

main(): creating thread, 0main(): creating thread, 1main(): creating thread, 2thread ID: 0message: this is messagethread ID: 1message: this is messagethread ID: 2message: this is messagemain(): creating thread, 3main(): creating thread, 4thread ID: 3message: this is messagethread ID: 4message: this is message

例3 pthread_join等待线程结束

#include<iostream>#include<cstdlib>#include<pthread.h>#include<unistd.h> //sleep(1)using namespace std;#define NUM_THREADS 5void *wait(void *t){    int i;    long tid;    tid = (long)t;    sleep(1);    cout << "Sleeping in thread" << endl;    cout << "Thread with id: " << tid << "...exiting" << endl;    pthread_exit(NULL);}int main(){    int rc;    int i;    pthread_t threads[NUM_THREADS];    pthread_attr_t attr;    void *status;    //Initialize and set thread joinable    pthread_attr_init(&attr);    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);    for(i = 0; i < NUM_THREADS; ++ i){        cout << "main(): creating thread, " << i << endl;        rc = pthread_create(&threads[i], NULL, wait, (void*)i);        if(rc){            cout << "error: " << rc << endl;        }    }    //free attribute and wait for the other threads    pthread_attr_destroy(&attr);    for(i = 0; i < NUM_THREADS; ++ i){        rc = pthread_join(threads[i], &status);        if(rc){            cout << "error: " << rc << endl;        }        cout << "Main: completed thread id: " << i;        cout << "exiting with status: " << status << endl;    }    cout << "Main: program exiting." << endl;    pthread_exit(NULL);}

输出结果:

main(): creating thread, 0main(): creating thread, 1main(): creating thread, 2main(): creating thread, 3main(): creating thread, 4Sleeping in threadSleeping in threadThread with id: 1...exitingThread with id: 4...exitingSleeping in threadSleeping in threadThread with id: 2...exitingThread with id: 0...exitingSleeping in threadThread with id: 3...exitingMain: completed thread id: 0exiting with status: 0Main: completed thread id: 1exiting with status: 0Main: completed thread id: 2exiting with status: 0Main: completed thread id: 3exiting with status: 0Main: completed thread id: 4exiting with status: 0Main: program exiting.

例4 pthread_exit() and pthread_join()

#define debug_Msg(fmt, arg...)\    do{\        printf("%s %d : ", __FILE__, __LINE__);\        printf(fmt, ##arg);\    }while(0)void * doPrint(void *arg){    debug_Msg("%s\n", (char*)arg);    char *p = "thread is over";    pthread_exit(p);}int main(){    pthread_t pid;    char * pt = "hello pthread";    pthread_create(&pid, NULL, doPrint, pt);    void * p = NULL;    pthread_join(pid, &p);    debug_Msg("return of thread : [%s]\n", (char*)p);    return 0;}