转 linux c多线程

linux下C语言多线程编程

原文链接:http://www.cnblogs.com/nanguabing/archive/2012/07/25/2608933.html
复制代码
#include <pthread.h>
#include <stdio.h>
#include <sys/time.h>
#include <string.h>
#define MAX 10
pthread_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;
}
复制代码

以上代码要做部分改动在前两个线程中thread1(void *)这里要加入void *,否则报错

执行结果:(这里编译时注意输入:gcc theard_first_test.cpp -o thread -lpthread)

本人转这篇文章时,发现pthread_create()函数的第四个参数,把NULL换成0可以发现输出有点不一样


这里是按照原博主的代码执行:

复制代码
我是主函数哦,我正在创建线程,呵呵
线程1被创建
线程2被创建
我是主函数哦,我正在等待线程完成任务阿,呵呵
thread1 : I'm thread 1
thread1 : number = 0
thread2 : I'm thread 2
thread2 : number = 1
thread1 : number = 2
thread2 : number = 3
thread1 : number = 4
thread2 : number = 5
thread1 : number = 6
thread1 : number = 7
thread2 : number = 8
thread1 : number = 9
thread2 : number = 10
thread1 :主函数在等我完成任务吗?
线程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);
}
int
main(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 exiting
thread 2 exit code 3
thread 1 returning
thread 1 exit code 2
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值