zoukankan      html  css  js  c++  java
  • 笔记1 linux 多线程 互斥锁

    //mutex lock
    #include<stdio.h>
    #include<unistd.h>
    #include<pthread.h>
    
    struct test
    {
        char a[10];
        char b[10];
        char c[10];
    }yb = {"111","222","33333"};
    
    static int j=0;
    
    pthread_mutex_t mutex_1 = PTHREAD_MUTEX_INITIALIZER;;
    
    void Print1(struct test *arg)
    {
        pthread_mutex_lock(&mutex_1);
        printf("a=%s,b=%s,c=%s,d=%X,j=%d
    ",arg->a,arg->b,arg->c,pthread_self(),j++);
        pthread_mutex_unlock(&mutex_1);
        pthread_exit((void *)j);
        printf("Never Coming.
    ");
    }
    
    
    int main()
    {
        pthread_t pid1,pid2,pid3;
        void *set;
    
        pthread_create(&pid1,NULL,&Print1,&yb);
        pthread_create(&pid2,NULL,&Print1,&yb);
        pthread_create(&pid3,NULL,&Print1,&yb);
    
        pthread_join(pid1,&set);
        printf("pid1 exit coed %d
    ",(int)set);
        pthread_join(pid2,&set);
        printf("pid2 exit coed %d
    ",(int)set);
        pthread_join(pid3,&set);
        printf("pid3 exit coed %d
    ",(int)set);
    
        sleep(1);
        return 0;
    }
  • 相关阅读:
    OpenGL入门1.3:着色器 GLSL
    OpenGL入门1.2:渲染管线简介,画三角形
    C++回调,函数指针
    JavaScript 比较和逻辑运算符
    JS 运算符
    JS 函数
    JS 对象
    JS 数据类型
    JS 变量
    JS 注释
  • 原文地址:https://www.cnblogs.com/sherlockhomles/p/4838314.html
Copyright © 2011-2022 走看看