zoukankan      html  css  js  c++  java
  • Linux C多线程学习

    /*************************************************************************
        > File Name: eg4.c
        > Author: 
        > Mail: 
        > Created Time: 2019年06月29日 星期六 10时56分11秒
     ************************************************************************/
    
    #include<stdio.h>
    #include<stdlib.h>
    #include<pthread.h>
    
    pthread_mutex_t lock;
    int s = 0;
    void* myfunc(void* args){
        int i = 0;
        for(i=0; i<1000000; i++){
            pthread_mutex_lock(&lock);
            s++;
            pthread_mutex_unlock(&lock);
        }
        return NULL;
    }
    int main(){
        pthread_t th1;
        pthread_t th2;
        pthread_mutex_init(&lock, NULL);
    
        pthread_create(&th1, NULL, myfunc, NULL);
        pthread_create(&th2, NULL, myfunc, NULL);
    
        pthread_join(th1, NULL);
        pthread_join(th2, NULL);
        printf("s=%d
    ", s);
    
    }

    多线程加锁

    /*************************************************************************
        > File Name: test1.c
        > Author: 
        > Mail: 
        > Created Time: 2019年06月28日 星期五 21时12分51秒
     ************************************************************************/
    
    #include<stdio.h>
    #include<stdlib.h>
    #include<pthread.h>
    
    void* myfunc(void* args){
        int i;
        char* name = (char*)args;
        for(i=1;i<50;i++){
            printf("%s:%d
    ", name, i);
        }
    
        return NULL;
    }
    
    
    int main(){
        pthread_t th1;
        pthread_t th2;
    
        pthread_create(&th1, NULL, myfunc, "th1");
        pthread_create(&th2, NULL, myfunc, "th2");
        pthread_join(th1, NULL);
        pthread_join(th2, NULL);
    
        return 0;
    }
    无欲则刚 关心则乱
  • 相关阅读:
    笔记
    BlangenOA项目展示(附源码)
    笔记截至20190406
    ASP.NET MVC 使用过滤器需要注意
    单例模式和HttpContext线程内唯一
    C#线程/进程同步(lock、Mutex、Semaphore)
    Web标准
    JavaScript 放置在文档最后面可以使页面加载速度更快
    GUI 面板实现 (解决了关闭事件)
    GUI 实现多个窗口 (使用封装特性)
  • 原文地址:https://www.cnblogs.com/xjyxp/p/11106085.html
Copyright © 2011-2022 走看看