线程同步

线程同步

例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); 

}

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

 

posted @ 2016-01-18 20:38  zhanglong71  阅读(123)  评论(0编辑  收藏  举报