zoukankan      html  css  js  c++  java
  • 多线程编程,线程同步,线程互斥示例

    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    #include <semaphore.h>
    int count,v1,v2;
    int flags = 1;
    sem_t sem;
    pthread_mutex_t mt;
    void *fun(void *arg)
    {
        int ret = -1;
        while(1)
        {
    #if 0
            ret = sem_wait(&sem);
            if ( -1 == ret )
            {
                perror("sem_init");
                return ;
            }
    #else
            ret = pthread_mutex_lock(&mt);
            if (0 != ret)
            {
                perror("pthread_mutex_lock");
                return -1;
            }
    
    #endif
            if ( v1 != v2 )
                printf("******* %d,%d
    ",v1,v2);
    #if 0
            ret = sem_post(&sem);
            if ( -1 == ret )
            {
                perror("sem_init");
                return ;
            }
    #else
            ret = pthread_mutex_unlock(&mt);
            if (0 != ret)
            {
                perror("pthread_mutex_lock");
                return -1;
            }
    #endif
        }
        *((int *)arg) = 3;
        pthread_exit("fun");//exit
    }
    int main(int argc,char **argv)
    {
        int a = 0;
        pthread_t id;
        int ret = -1;
        void * val = NULL;
    #if 0
        ret = sem_init(&sem,0,1);
        if ( -1 == ret )
        {
            perror("sem_init");
            return -1;
        }
    #else  
        ret = pthread_mutex_init(&mt,NULL);
        if( 0 != ret)
        {
            perror("pthread_mutex_init");
            return -1;
        }
    #endif
        ret = pthread_create(&id,NULL,fun,(void*)&a);//fork
        if ( 0 != ret )
        {
            perror("pthread_creat");
            return -1;
        }
    
        while(1)
        {
    #if 0
            ret = sem_wait(&sem);
            if ( -1 == ret )
            {
                perror("sem_init");
                return -1;
            }
    #else 
            ret = pthread_mutex_lock(&mt);
            if (0 != ret)
            {
                perror("pthread_mutex_lock");
                return -1;
            }
    #endif
            count++;
            v1=count;
            v2=count;
    #if 0
            ret = sem_post(&sem);
            if ( -1 == ret )
            {
                perror("sem_init");
                return -1;
            }
    #else 
            ret = pthread_mutex_unlock(&mt);
            if (0 != ret)
            {
                perror("pthread_mutex_lock");
                return -1;
            }
    #endif
        }
        ret = pthread_join(id,&val);//wait
        if ( 0 != ret )
        {
            perror("pthread_join");
            return -1;
        }
        printf("return val is %s
    ",val);
        printf("%d**
    ",a);
        return 0;
    }
  • 相关阅读:
    PostCSS: 跨浏览器兼容性
    js延迟执行函数
    angularjs怎么做动jq中toggle (ng-toggle in AngularJS )
    elasticsearch权威指南
    docker 运行 elasticsearch + kibana + head 集群
    RabbitMQ 的基本介绍
    docker 安装rabbitMQ
    Docker WARNING: IPv4 forwarding is disabled. Networking will not work.
    nginx 的 负载均衡
    Nginx 的 docker 部署
  • 原文地址:https://www.cnblogs.com/smile-at-you/p/3363788.html
Copyright © 2011-2022 走看看