• linux 多线程那点事


    说明:对多线程与相互排斥锁不熟悉的请參考其他


    #include <pthread.h>
    #include <stdio.h>
    #include <stdlib.h>
    pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;/*初始化相互排斥锁*/
    pthread_cond_t cond = PTHREAD_COND_INITIALIZER;/*初始化条件变量*/
    void *thread1(void *);
    void *thread2(void *);
    int i=1;
    int main(void)
    {
        pthread_t t_a;
        pthread_t t_b;
        pthread_create(&t_a,NULL,thread1,(void *)NULL);/*创建进程t_a*/
        pthread_create(&t_b,NULL,thread2,(void *)NULL); /*创建进程t_b*/
        pthread_join(t_a, NULL);/*等待进程t_a结束 和windows的waitForSingleObject 相似*/
        pthread_join(t_b, NULL);/*等待进程t_b结束 和windows的waitForSingleObject 相似*/
        /* 释放资源*/
        pthread_mutex_destroy(&mutex);
        pthread_cond_destroy(&cond);
        exit(0);
    }
    void *thread1(void *junk)
    {
        for(i=1;i<=6;i++)
        {
    	printf("now i = %d
    ", i);
            pthread_mutex_lock(&mutex);/*锁住相互排斥量*/
    		printf("thread1: lock %d
    ", __LINE__);
            if(i%3==0){
    			printf("thread1:signal 1  %d
    ", __LINE__);
                pthread_cond_signal(&cond);/*条件改变,发送信号,通知t_b进程*/
    			printf("thread1:signal 2  %d
    ", __LINE__);
    			sleep(1);
    		}
            pthread_mutex_unlock(&mutex);/*解锁相互排斥量*/
    		printf("thread1: unlock %d
    
    ", __LINE__);
    		sleep(1);
    	}
    }
    void *thread2(void *junk)
    {
        while(i<6)
        {
            pthread_mutex_lock(&mutex);
    		printf("thread2: lock %d
    ", __LINE__);
    		if(i%3!=0){
    			printf("thread2: wait 1  %d
    ", __LINE__);
    	    /*pthread_cond_wait 函数将解锁mutex。并使当前线程堵塞在cond指向的条件变量上*/	
                pthread_cond_wait(&cond,&mutex);/*解锁mutex,并等待cond改变*/
    			printf("thread2: wait 2  %d
    ", __LINE__);;
    		}
            pthread_mutex_unlock(&mutex);
    		printf("thread2: unlock %d
    
    ", __LINE__);
    		sleep(1);
    	}
    }
    



    root@ubuntu:/usr/syw/linux/Thread# gcc -g threadDemo3_arg.c -o thread3 -lpthread

    root@ubuntu:/usr/syw/linux/Thread# ./thread3


    now i = 1
    thread2: lock 44
    thread2: wait 1  46
    thread1: lock 27
    thread1: unlock 35

    now i = 2
    thread1: lock 27
    thread1: unlock 35

    now i = 3
    thread1: lock 27
    thread1:signal 1  29
    thread1:signal 2  31
    thread1: unlock 35

    thread2: wait 2  49
    thread2: unlock 52

    now i = 4
    thread2: lock 44
    thread2: wait 1  46
    thread1: lock 27
    thread1: unlock 35

    now i = 5
    thread1: lock 27
    thread1: unlock 35

    now i = 6
    thread1: lock 27
    thread1:signal 1  29
    thread1:signal 2  31
    thread1: unlock 35

    thread2: wait 2  49
    thread2: unlock 52


  • 相关阅读:
    使用C# 实现串口拨号器的SIM卡通信
    物联网协议Coap协议介绍
    C#实现简单的串口通信
    C#硬件访问(摄像头、麦克风)
    请问在电脑里PNP是什么意思啊?
    原码,反码,补码,及Java中数字表示方法
    3_PHP表达式_5_数据类型转换_类型强制转换
    3_PHP表达式_4_PHP运算符
    3_PHP表达式_5_数据类型转换_类型自动转换
    3_PHP表达式_3_有关变量或常量状态的函数
  • 原文地址:https://www.cnblogs.com/cynchanpin/p/7249707.html
Copyright © 2020-2023  润新知