美文网首页
Linux mutex 编程

Linux mutex 编程

作者: louyang | 来源:发表于2017-12-19 10:09 被阅读41次

两个并行运行的程序片断,如线程,同时读写同一块内存,其结果是不确定的。这时候我们需要互斥锁,保证一个线程先读写完,然后另一个线程才可以读写。

1 没有使用mutex的例子
# cat mutex-example-1.c

#include <stdio.h>    //ptrinf
#include <pthread.h>  //pthread_xxx
#include <unistd.h>   //sleep

int global_para = 0;

void *do_sth(void *p)
{
   long i = (long)p;

   printf("thread %d - start to read\n", i);
   int temp = global_para;
   sleep(2);
   printf("thread %d - end of reading\n", i);

   printf("thread %d - start to write\n", i);
   sleep(2);
   global_para = temp + (int)i;
   printf("thread %d - end of reading\n", i);
}

int main()
{
   pthread_t t1, t2;

   pthread_create(&t1, 0, do_sth, (void *)1);
   pthread_create(&t2, 0, do_sth, (void *)2);

   pthread_join(t1, NULL);
   pthread_join(t2, NULL);

   printf("-- global_para is %d --\n", global_para);
}
# gcc mutex-example-1.c -o mutex-example-1 -lpthread && ./mutex-example-1
thread 2 - start to read
thread 1 - start to read
thread 1 - end of reading
thread 1 - start to write
thread 2 - end of reading
thread 2 - start to write
thread 1 - end of reading
thread 2 - end of reading
-- global_para is 2 --

每一次的运行结果都可能不一样。

# gcc mutex-example-1.c -o mutex-example-1 -lpthread && ./mutex-example-1
thread 2 - start to read
thread 1 - start to read
thread 2 - end of reading
thread 2 - start to write
thread 1 - end of reading
thread 1 - start to write
thread 2 - end of reading
thread 1 - end of reading
-- global_para is 1 --
2 使用mutex的例子
# cat mutex-example-2.c

#include <stdio.h>    //ptrinf,perror
#include <stdlib.h>   //exit
#include <pthread.h>  //pthread_xxx
#include <unistd.h>   //sleep

int global_para = 0;
pthread_mutex_t lock;

void die(char *s)
{
   perror(s);
   exit(1);
}

void *do_sth(void *p)
{
   long i = (long)p;

   pthread_mutex_lock(&lock);

   printf("thread %d - start to read\n", i);
   int temp = global_para;
   sleep(2);
   printf("thread %d - end of reading\n", i);

   printf("thread %d - start to write\n", i);
   sleep(2);
   global_para = temp + (int)i;
   printf("thread %d - end of reading\n", i);

   pthread_mutex_unlock(&lock);
}

int main()
{
   pthread_t t1, t2;

   if (pthread_mutex_init(&lock, NULL) != 0)
   {
      die("pthread_mutex_init()");
   }

   pthread_create(&t1, 0, do_sth, (void *)1);
   pthread_create(&t2, 0, do_sth, (void *)2);

   pthread_join(t1, NULL);
   pthread_join(t2, NULL);
   pthread_mutex_destroy(&lock);

   printf("-- global_para is %d --\n", global_para);
}
# gcc mutex-example-2.c -o mutex-example-2 -lpthread && ./mutex-example-2
thread 2 - start to read
thread 2 - end of reading
thread 2 - start to write
thread 2 - end of reading
thread 1 - start to read
thread 1 - end of reading
thread 1 - start to write
thread 1 - end of reading
-- global_para is 3 --

因为是串行读写,程序运行慢了很多,但结果正确了。

参考

http://www.thegeekstuff.com/2012/05/c-mutex-examples/?refcom

相关文章

  • Linux mutex 编程

    两个并行运行的程序片断,如线程,同时读写同一块内存,其结果是不确定的。这时候我们需要互斥锁,保证一个线程先读写完,...

  • linux编程-线程

    linux编程-线程 MUTEX 一.概述 互斥量是线程同步的一...

  • mutex lock 唤醒顺序

    在 Linux 多线程编程中,我们常常用 pthread_mutex_lock 来做线程间同步。当锁被占用时,当前...

  • 互斥锁

    Linux线程-互斥锁pthread_mutex_t

  • 25. Mutex

    25. Mutex 临界区 在学习 Mutex 之前,我们需要理解并发编程中临界区(Critical Sectio...

  • Linux内核同步机制-互斥锁

    一、初始化 互斥锁(mutex)定义文件:include/linux/mutex.h,方法如下: 1、静态定义并初...

  • Linux系统编程 —互斥量mutex

    互斥量mutex 前文提到,系统中如果存在资源共享,线程间存在竞争,并且没有合理的同步机制的话,会出现数据混乱的现...

  • 自定义同步组件构建实例及AbstractQueuedSynchr

    以下知识参考《Java并发编程的艺术》 类Mutex就是一个自定义的同步组件,在Mutex中定义了Sync静态内部...

  • Linux线程同步

    互斥量mutex Linux中提供一把互斥锁mutex(也称之为互斥量)。 每个线程在对资源操作前都尝试先加锁,成...

  • Linux网络编程篇之ICMP协议分析及ping程序实现

    Linux网络编程系列: Linux网络编程篇之Socket编程预备知识 Linux网络编程篇之TCP协议分析及聊...

网友评论

      本文标题:Linux mutex 编程

      本文链接:https://www.haomeiwen.com/subject/hwfbwxtx.html