Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1419794
  • 博文数量: 1334
  • 博客积分: 645
  • 博客等级: 上士
  • 技术积分: 5762
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-25 16:56
文章分类

全部博文(1334)

文章存档

2014年(108)

2013年(1059)

2012年(169)

分类: LINUX

2013-06-05 14:46:32

原文地址:(2)互斥锁 作者:g_programming

互斥锁:

互斥锁定义:pthread_mutex_t

互斥锁初始化: PTHREAD_MUTEX_INITIALIZER  

 注:仅使用于静态分配的互斥锁

             或者以下面方式初始化,但要使用相应的注销函数:

#include

1. int pthread_mutex_init(pthread_mutex_t *restrict mutex,

const pthread_mutexattr_t *restrict attr);

2. int pthread_mutex_destroy(pthread_mutex_t *mutex);

             Both return: 0 if OK, error number on failure

3. Int pthread_mutex_lock(pthread_mutex_t *mutex)

4. int pthread_mutex_trylock(pthread_mutex_t *mutex)

5. int pthread_mutex_unlock(pthread_mutex_t *mutex)

    返回值:成功返回0, 否则返回错误编号

6void usleep(int micro_seconds);把进程挂起一段时间, 单位是微秒(百万分之一秒); 头文件unistd.h

 

互斥量从本质上说就是一把锁, 提供对共享资源的保护访问.在下面这个例子中,buf[512]为共享资源,当一个线程对其操作时可能其他线程要使用它,所以必须加锁保护当前只有一个线程使用。

***************************** 例子 11.1******************************

线程的互斥锁的使用例程

*********************************************************************

#include

#include

#include

 

pthread_mutex_t flock;

char buf[512];// 共享资源

 

void *

thr_fn1(void *arg)

{

      long i;

     for(i=0;i<2000000;i++)

     {

         pthread_mutex_lock(&flock);

         strcpy(buf,"aaaaaaaaaaaaaaaaaaaa\n");

         strcpy(buf,"bbbbbbbbbbbbbbbbbbbb\n");

         strcpy(buf,"cccccccccccccccccccc\n");

         strcpy(buf,"dddddddddddddddddddd\n");

         pthread_mutex_unlock(&flock);

     }

}

 

void *

thr_fn2(void *arg)

{

        int i;

        for(i=0;i<10;i++)

        {

            pthread_mutex_lock(&flock);

            printf(buf);

            pthread_mutex_unlock(&flock);

            usleep(5);

        }

}

 

int main()

{

        pthread_t tid1,tid2;

        int err;

 

        strcpy(buf,"hello,world!\n");

        pthread_mutex_init(&flock,NULL);

 

        err=pthread_create(&tid1,NULL,thr_fn1,NULL);

        if(err!=0)

                printf("can't create thread 1:%s\n",strerror(err));

        err=pthread_create(&tid2,NULL,thr_fn2,NULL);

        if(err!=0)

                printf("can't create thread 1:%s\n",strerror(err));

 

        pthread_join(tid1,NULL);

        pthread_join(tid2,NULL);

 

        pthread_mutex_destroy(&flock);

        return 0;

}

*********************************************************************

输出结果:

dddddddddddddddddddd

dddddddddddddddddddd

dddddddddddddddddddd

dddddddddddddddddddd

dddddddddddddddddddd

dddddddddddddddddddd

dddddddddddddddddddd

dddddddddddddddddddd

dddddddddddddddddddd

dddddddddddddddddddd

 

线程不用互斥锁的使用例程

#include

#include

#include

 

char buf[512];

 

void *

thr_fn1(void *arg)

{  

 long i;

 for(i=0;i<2000000;i++)

 {

strcpy(buf,"aaaaaaaaaaaaaaaaaaaa\n");

strcpy(buf,"bbbbbbbbbbbbbbbbbbbb\n");

strcpy(buf,"cccccccccccccccccccc\n");

strcpy(buf,"dddddddddddddddddddd\n");

 }

}

 

void *

thr_fn2(void *arg)

{

 int i;

 for(i=0;i<10;i++)

{

 printf(buf);

 usleep(5);

}

}

 

int main()

{

 pthread_t tid1,tid2;

 int err;

 

strcpy(buf,"hello,world!\n");

err=pthread_create(&tid1,NULL,thr_fn1,NULL);

 if(err!=0)

 printf("can't create thread 1:%s\n",strerror(err));

 err=pthread_create(&tid2,NULL,thr_fn2,NULL);

 if(err!=0)

printf("can't create thread 1:%s\n",strerror(err));

 

pthread_join(tid1,NULL);

 pthread_join(tid2,NULL);

 

return 0;

}

输出结果:

cccccccccccccccccccc

bbbbbbbbbbbbbbbbbaaa

bbbbbbbbbbbbbbbbbbbb

dddddddddddddddddddd

dddddddddddddddddddd

dddddddddddddddddddd

dddddddddddddddddddd

dddddddddddddddddddd

dddddddddddddddddddd

dddddddddddddddddddd

 

线程用互斥锁保护结构的例程

此例对结构的保护主要是在还有线程使用该结构之前不能释放分配的空间

#include

#include

 

pthread_mutex_t flock;

 

struct foo{

    int f_count;

    pthread_mutex_t f_lock;

    char buf[512];

};

struct foo *

foo_alloc(void)

{

    struct foo *fp;

    if((fp = malloc(sizeof(struct foo))) != NULL)

    {

       fp->f_count = 1;

       if(pthread_mutex_init(&fp->f_lock, NULL) !=0)

       {

           free(fp); 

           return NULL;

       }

    }

    return fp;

}

void

foo_hold(struct foo *fp)

{

    pthread_mutex_lock(&fp->f_lock);

    fp->f_count++;

    pthread_mutex_unlock(&fp->f_lock);

}

void

foo_rele(struct foo *fp)

{

    pthread_mutex_lock(&fp->f_lock);

    if(-- fp->f_count == 0){

       pthread_mutex_unlock(&fp->f_lock);

       pthread_mutex_destroy(&fp->f_lock);

       free(fp);

    }

    else

    {

       pthread_mutex_unlock(&fp->f_lock);

    }

}

 

void *

thr_fn1(void *arg)

{      

 long i;

 struct foo *fp = (struct foo *)arg;

 for(i=0;i<2000000;i++)

 {

    pthread_mutex_lock(&flock);

 

    foo_hold(fp);

          

    strcpy(fp->buf,"aaaaaaaaaaaaaaaaaaaa\n");

    strcpy(fp->buf,"bbbbbbbbbbbbbbbbbbbb\n");

    strcpy(fp->buf,"cccccccccccccccccccc\n");

    strcpy(fp->buf,"dddddddddddddddddddd\n");

    foo_rele(fp);

    pthread_mutex_unlock(&flock);

 

 }

}

void *

thr_fn2(void *arg)

{

 int i;

 struct foo *fp = (struct foo *)arg;

 for(i=0;i<10;i++)

 {

     pthread_mutex_lock(&flock);

     foo_hold(fp);

     printf(fp->buf);

     foo_rele(fp);

     pthread_mutex_unlock(&flock);

     usleep(5);

 }

}

                                                                               

int main()

{

 pthread_t tid1,tid2;

 int err;

 struct foo *fp;

 

 fp = foo_alloc();                                                                              

 strcpy(fp->buf,"hello,world!\n");

 printf("%s", fp->buf);                                                                               

 err=pthread_create(&tid1,NULL,thr_fn1,(void *)fp);

 if(err!=0)

 printf("can't create thread 1:%s\n",strerror(err));

 err=pthread_create(&tid2,NULL,thr_fn2,(void *)fp);

 if(err!=0)

 printf("can't create thread 1:%s\n",strerror(err));

                                                                                

 pthread_join(tid1,NULL);

 pthread_join(tid2,NULL);

                                                                               

 return 0;

}

 

 

结果:

hello,world!

dddddddddddddddddddd

dddddddddddddddddddd

dddddddddddddddddddd

dddddddddddddddddddd

dddddddddddddddddddd

dddddddddddddddddddd

dddddddddddddddddddd

dddddddddddddddddddd

dddddddddddddddddddd

dddddddddddddddddddd

 

 

 

 

 

阅读(224) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~