无锁编程(五) - RCU(Read-Copy-Update)
来源:互联网 发布:淘宝让买家改评价 编辑:程序博客网 时间:2024/06/06 06:46
RCU(Read-Copy Update)
RCU就是指读-拷贝修改,它是基于其原理命名的。对于被RCU保护的共享数据结构,读操作不需要获得任何锁就可以访问,但写操作在访问它时首先拷贝一个副本,然后对副本进行修改,最后在适当的时机把指向原来数据的指针重新指向新的被修改的数据。这个时机就是所有引用该数据的CPU都退出对共享数据的操作。
Linux内核中内存管理大量的运用到了RCU机制。为每个内存对象增加了一个原子计数器用来继续该对象当前访问数。当没有其他进程在访问该对象时(计数器为0),才允许回收该内存。
从这个流程可以看出,RCU类似于一种读写锁的优化,用于解决读和写之间的同步问题。比较适合读多,写少的情况,当写操作过多的时候,这里的拷贝和修改的成本同样也很大。(写操作和写操作之间的同步还需要其它机制来保证)。
代码讲解:
#include <stdio.h>#include <pthread.h>#include <unistd.h>#include <stdlib.h>#include <string.h>pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;int currentidx = 0;char* str[2] = {0};void* consume(void *arg){ sleep(1); while(1) { printf("************************consumed %s, index %d, self %d\n",str[currentidx], currentidx, pthread_self()); sleep(1); } return NULL;}void* produce( void * arg ){ const char* s_str1 = "hello"; const char* s_str2 = "world"; while(1) {printf("product begin\n");// read copy int other = 1 - currentidx;str[other] = (char*)malloc(6);if (other == 0){strncpy(str[other], s_str1, 6);}else{strncpy(str[other], s_str2, 6);}// update原子的修改索引currentidx = other;// delete old currentidxfree(str[1-currentidx]); sleep(5); } return NULL;}int main( void ){ pthread_t thread1,thread2; pthread_create(&thread1, NULL, &produce, NULL ); pthread_create(&thread2, NULL, &consume, NULL ); pthread_join(thread1,NULL); pthread_join(thread2,NULL); return 0;}
结果说明:
[root@rocket lock-free]# ./lockfree_rcu
product begin
************************consumed world, index1, self 1395513088
************************consumed world, index1, self 1395513088
************************consumed world, index1, self 1395513088
************************consumed world, index1, self 1395513088
product begin
************************consumed hello, index0, self 1395513088
************************consumed hello, index0, self 1395513088
************************consumed hello, index0, self 1395513088
************************consumed hello, index0, self 1395513088
************************consumed hello, index0, self 1395513088
product begin
************************consumed world, index1, self 1395513088
************************consumed world, index1, self 1395513088
************************consumed world, index1, self 1395513088
************************consumed world, index1, self 1395513088
************************consumed world, index1, self 1395513088
- 无锁编程(五) - RCU(Read-Copy-Update)
- RCU(Read-Copy Update)
- RCU(Read-Copy Update)
- RCU (Read-Copy Update) 的实现机制
- Linux内核RCU(Read Copy Update)锁简析
- Linux内核RCU(Read Copy Update)锁简析-前传
- RCU(Read-Copy Update)synchronize原理分析
- 不可睡眠锁:RCU read-copy-updat
- Read-copy-update
- read copy update
- 使用RCU技术实现读写线程无锁
- RCU锁
- RCU 锁
- XML编程(CRUD)-create read update delete(DOM解析)
- rcu锁机制
- linux RCU锁机制。
- RCU锁机制
- linux RCU锁机制
- Linux 磁阵性能瓶颈定位过程
- Mybatis之配置文件中的objectFactory节点内容说明
- 欢迎使用CSDN-markdown编辑器
- iOS开发中常用的轮子 第四篇 抽屉和侧滑效果
- Spring 漏洞分析
- 无锁编程(五) - RCU(Read-Copy-Update)
- IOS UILabel
- LeetCode(52) Power of Two
- 按enter键提交表单
- leetcode | Merge Sorted Array
- 文章标题
- Android 位置服务——BaiduLocation的使用
- Myeclipse_Pro_GA_2014中的服务器视图(Servers)中如何去掉讨厌的MyEclipse Derby选项
- Android中运行的错误:java.lang.UnsatisfiedLinkError: Couldn't load locSDK3: findLibrary returned null.