linux下C语言多线程编程

来源:互联网 发布:尤易软件 编辑:程序博客网 时间:2024/04/30 05:51


#include <pthread.h>#include <stdio.h>#include <sys/time.h>#include <string.h>#define MAX 10pthread_t thread[2];pthread_mutex_t mut;int number=0, i;void *thread1(){        printf ("thread1 : I'm thread 1\n");        for (i = 0; i < MAX; i++)        {                printf("thread1 : number = %d\n",number);                pthread_mutex_lock(&mut);                        number++;                pthread_mutex_unlock(&mut);                sleep(2);        }        printf("thread1 :主函数在等我完成任务吗?\n");        pthread_exit(NULL);}void *thread2(){        printf("thread2 : I'm thread 2\n");        for (i = 0; i < MAX; i++)        {                printf("thread2 : number = %d\n",number);                pthread_mutex_lock(&mut);                        number++;                pthread_mutex_unlock(&mut);                sleep(3);        }        printf("thread2 :主函数在等我完成任务吗?\n");        pthread_exit(NULL);}void thread_create(void){        int temp;        memset(&thread, 0, sizeof(thread));          //comment1        /*创建线程*/        if((temp = pthread_create(&thread[0], NULL, thread1, NULL)) != 0)       //comment2                printf("线程1创建失败!\n");        else                printf("线程1被创建\n");        if((temp = pthread_create(&thread[1], NULL, thread2, NULL)) != 0)  //comment3                printf("线程2创建失败");        else                printf("线程2被创建\n");}void thread_wait(void){        /*等待线程结束*/        if(thread[0] !=0) {                   //comment4                pthread_join(thread[0],NULL);                printf("线程1已经结束\n");        }        if(thread[1] !=0) {                //comment5                pthread_join(thread[1],NULL);                printf("线程2已经结束\n");        }}int main(){        /*用默认属性初始化互斥锁*/        pthread_mutex_init(&mut,NULL);        printf("我是主函数哦,我正在创建线程,呵呵\n");        thread_create();        printf("我是主函数哦,我正在等待线程完成任务阿,呵呵\n");        thread_wait();        return 0;}
 
在编译中要加 -lpthread参数
    gcc thread.c -o thread -lpthread

执行结果

我是主函数哦,我正在创建线程,呵呵线程1被创建线程2被创建我是主函数哦,我正在等待线程完成任务阿,呵呵thread1 : I'm thread 1thread1 : number = 0thread2 : I'm thread 2thread2 : number = 1thread1 : number = 2thread2 : number = 3thread1 : number = 4thread2 : number = 5thread1 : number = 6thread1 : number = 7thread2 : number = 8thread1 : number = 9thread2 : number = 10thread1 :主函数在等我完成任务吗?线程1已经结束thread2 :主函数在等我完成任务吗?线程2已经结束

下面一个稍微复杂的多线程

extern int pthread_join __P ((pthread_t __th, void **__thread_return));
  第一个参数为被等待的线程标识符,第二个参数为一个用户定义的指针,它可以用来存储被等待线程的返回值。这个函数是一个线程阻塞的函数,调用它的线程将一直等待到被等待的线程结束为止,当函数返回时,被等待线程的资源被收回。一个线程的结束有两种途径,一种是象我们上面的例子一样,函数结束了,调用它的线程也就结束了;另一种方式是通过函数pthread_exit来实现。它的函数原型为:
  extern void pthread_exit __P ((void *__retval)) __attribute__ ((__noreturn__));
  唯一的参数是函数的返回代码,只要pthread_exit中的参数retval不是NULL,这个值将被传递给 thread_return。最后要说明的是,一个线程不能被多个线程等待,否则第一个接收到信号的线程成功返回,其余调用pthread_join的线程则返回错误代码ESRCH。

实例:


#include <stdio.h>#include <pthread.h>#include <stdlib.h>pthread_t       tid1, tid2; void            *tret; void *thr_fn1(void *arg){        sleep(1);//睡眠一秒,等待TID2结束。        pthread_join(tid2, &tret);//tid1一直阻赛,等到tid2的退出,获得TID2的退出码         printf("thread 2 exit code %d\n", (int)tret);    printf("thread 1 returning\n");    return((void *)2);}void *thr_fn2(void *arg){          printf("thread 2 exiting\n");     pthread_exit((void *)3);}intmain(void){    int            err;    err = pthread_create(&tid1, NULL, thr_fn1, NULL);    if (err != 0)        printf("can't create thread 1\n");    err = pthread_create(&tid2, NULL, thr_fn2, NULL);    if (err != 0)        printf("can't create thread 2\n");    err = pthread_join(tid1, &tret);//祝线程一直阻赛,等待TID1的返回。    if (err != 0)        printf("can't join with thread 1\n");    printf("thread 1 exit code %d\n", (int)tret);      //err = pthread_join(tid2, &tret);    //if (err != 0)    //    printf("can't join with thread 2\n");//    printf("thread 2 exit code %d\n", (int)tret);    exit(0);} 命令:#gcc -lthread myfile11-3.c        :#./a.out运行结果:thread 2 exitingthread 2 exit code 3thread 1 returningthread 1 exit code 2
复制代码
原创粉丝点击