向内存中写数据

 1 // SharedMemorySample_write_main.cpp
2 #include <SDKDDKVer.h>
3 #include <Windows.h>
4 #include <stdio.h>
5
6 int main(int argc, char* argv[])
7 {
8 int shmem_size = 16; // 16byte
9 HANDLE shmem = INVALID_HANDLE_VALUE;
10 HANDLE mutex = INVALID_HANDLE_VALUE;
11
12 mutex = ::CreateMutex(NULL, FALSE, "mutex_sample_name");
13
14 shmem = ::CreateFileMapping(
15 INVALID_HANDLE_VALUE,
16 NULL,
17 PAGE_READWRITE,
18 0,
19 shmem_size,
20 "shared_memory_name"
21 );
22
23 char *buf = (char*)::MapViewOfFile(shmem, FILE_MAP_ALL_ACCESS, 0, 0, shmem_size);
24
25
26 for (unsigned int c = 0; c < 60; ++c) {
27 // mutex lock
28 WaitForSingleObject(mutex, INFINITE);
29
30 // write shared memory
31 memset(buf, c, shmem_size);
32
33 printf("write shared memory...c=%d\n", c);
34
35 // mutex unlock
36 ::ReleaseMutex(mutex);
37
38 ::Sleep(1000);
39 }
40
41 // release
42 ::UnmapViewOfFile(buf);
43 ::CloseHandle(shmem);
44 ::ReleaseMutex(mutex);
45
46 return 0;
47 }

从内存中读数据

 1 // SharedMemorySample_read_main.cpp
2 #include <SDKDDKVer.h>
3 #include <Windows.h>
4 #include <stdio.h>
5
6 int main(int argc, char* argv[])
7 {
8 int shmem_size = 16; // 16byte
9 HANDLE shmem = INVALID_HANDLE_VALUE;
10 HANDLE mutex = INVALID_HANDLE_VALUE;
11
12 mutex = ::CreateMutex(NULL, FALSE, "mutex_sample_name");
13
14 shmem = ::CreateFileMapping(
15 INVALID_HANDLE_VALUE,
16 NULL,
17 PAGE_READWRITE,
18 0,
19 shmem_size,
20 "shared_memory_name"
21 );
22
23 char *buf = (char*)::MapViewOfFile(shmem, FILE_MAP_ALL_ACCESS, 0, 0, shmem_size);
24
25
26 for (unsigned int c = 0; c < 60; ++c) {
27 // mutex lock
28 WaitForSingleObject(mutex, INFINITE);
29
30 printf("read shared memory...c=%d\n", buf[0]);
31
32 // mutex unlock
33 ::ReleaseMutex(mutex);
34
35 ::Sleep(1000);
36 }
37
38 // release
39 ::UnmapViewOfFile(buf);
40 ::CloseHandle(shmem);
41 ::ReleaseMutex(mutex);
42
43 return 0;
44 }

最新文章

  1. linux下VNC的配置及使用
  2. MYSQL中创建存储过程实现向表中循环插入数据
  3. session与cookie的区别---
  4. 在C#中开启事务
  5. 如何在win7下配置IIS?
  6. UIProgressView[进度条][一般型];UIStepper步数器][事件驱动型]
  7. jQuery.hhLRSlider 左右滚动图片插件
  8. 阿里云部署Docker(7)----将容器连接起来
  9. JAVA实例变量的初始化过程
  10. 14、Cocos2dx 3.0三,找一个小游戏开发Scene and Layer:游戏梦想
  11. 不再迷惑,无值和NULL值的转换
  12. 201521123064 《Java程序设计》第8周学习总结
  13. 【批处理学习笔记】第十二课:常用DOS命令(2)
  14. technologies
  15. vue 页面跳转的两种方式
  16. AndroidStudio下加入百度地图的使用 (三)——API基本方法及常量属性
  17. 手动下载python更新后 换回以前版本
  18. .Net拾忆:HttpWebRequest/WebClient两种方式模拟Post
  19. Java内置锁synchronized的实现原理
  20. SourceTree 全局忽略及相关问题

热门文章

  1. kafka集群under replicated分析
  2. python 通过win32com操作vcf到outlook中,同时解决乱码问题
  3. 简谈CPU峰值性能怎么计算[转载]
  4. Python3开启自带http服务
  5. Python的入门学习Day 25~27——form”夜曲编程“
  6. TP开发项目时遇到的问题记录
  7. Maven中的元素Exclusions、modules、parent、properties以及import
  8. Docker CLI docker attach 常用命令
  9. java8 利用 ConcurrentHashMap list根据 某个属性 去重
  10. drf Serializer基本使用