首页 > 编程语言 > 详细

线程同步

时间:2016-01-18 22:22:37      阅读:274      评论:0      收藏:0      [点我收藏+]

线程同步

例1: 通过互斥量实现线程同步

mymutex.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>

//gcc -g mymutex.c -o mymutex -lpthread

#define ERROR(flag,msg)            if(flag)                        {                                    printf("%d: ",__LINE__);        fflush(stdout);                    perror(msg);                    exit(errno);                }

int flag = 0;
//pthread_mutex_t mutex;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;


void *write1(void *fp)
{
    while(1)
    {
        pthread_mutex_lock(&mutex);
        if(flag % 2 == 0)
        {
            flag++;
            if(flag > 10)
            {
                pthread_mutex_unlock(&mutex);
                break;
            }
            fprintf(fp,"thread 111 flag = %d\n",flag);            
        }
        pthread_mutex_unlock(&mutex);
    }
}

void *write2(void *fp)
{
    while(1)
    {
        pthread_mutex_lock(&mutex);
        if(flag % 2 == 1)
        {
            flag++;
            if(flag > 10)
            {
                pthread_mutex_unlock(&mutex);
                break;
            }
            fprintf(fp,"thread 222 flag = %d\n",flag);            
        }        
        pthread_mutex_unlock(&mutex);
    }
}

int main(int argc, char *argv[])
{
    pthread_t tid1;
    pthread_t tid2;

    FILE *fp = fopen(argv[1],"w");
    
//    pthread_mutex_init(&mutex,NULL);

    pthread_create(&tid1,NULL,write1,fp);
    pthread_create(&tid2,NULL,write2,fp);

    pthread_join(tid1,NULL);
    pthread_join(tid2,NULL);

//    pthread_mutex_destroy(&mutex);

    fclose(fp);

    return 0;
}

编译链接执行, 输出如下:

技术分享

例2: 通过互斥量和条件量实现线程同步

mycondition.c

#include <stdio.h> 
#include <unistd.h>
#include <pthread.h>  

//gcc -g mycondition.c -o mycondition -lpthread

pthread_mutex_t    mutex;
pthread_cond_t    cond; 

void *thread1(void *arg) 
{ 
    pthread_cleanup_push (pthread_mutex_unlock, &mutex); 
    while(1)
    { 
        pthread_mutex_lock (&mutex);         
        printf ("thread1 is running\n"); 
        
        pthread_cond_wait(&cond, &mutex);     
        printf ("thread 111111111111111111111111\n"); 
        
        pthread_mutex_unlock (&mutex);         
        sleep (2); 
    } 
    pthread_cleanup_pop (0); 
} 

void *thread2(void *arg) 
{ 
    while(1)
    { 
        pthread_mutex_lock (&mutex);     
        printf ("thread2 is running\n"); 
        
        pthread_cond_wait (&cond, &mutex);         
        printf ("thread 2222222222222222222222222\n"); 
        
        pthread_mutex_unlock (&mutex);         
        sleep (1); 
    } 
}
 
int main(void) 
{ 
    pthread_t tid1, tid2; 

    pthread_mutex_init (&mutex, NULL); 
    pthread_cond_init (&cond, NULL); 
    
    pthread_create (&tid1, NULL, (void *) thread1, NULL); 
    pthread_create (&tid2, NULL, (void *) thread2, NULL); 

    do 
    { 
        pthread_cond_broadcast(&cond);
//        pthread_cond_signal(&cond); 
        sleep(1);
    } while (1); 

}

编译链接执行, 输出如下:

技术分享

 

线程同步

原文:http://www.cnblogs.com/zhanglong71/p/5137944.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!