c_cpp WIN32的SharedMemory示例
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp WIN32的SharedMemory示例相关的知识,希望对你有一定的参考价值。
#include <SDKDDKVer.h>
#include <Windows.h>
#include <stdio.h>
int main(int argc, char* argv[])
{
int shmem_size = 16; // 16byte
HANDLE shmem = INVALID_HANDLE_VALUE;
HANDLE mutex = INVALID_HANDLE_VALUE;
mutex = ::CreateMutex(NULL, FALSE, "mutex_sample_name");
shmem = ::CreateFileMapping(
INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
0,
shmem_size,
"shared_memory_name"
);
char *buf = (char*)::MapViewOfFile(shmem, FILE_MAP_ALL_ACCESS, 0, 0, shmem_size);
for (unsigned int c = 0; c < 60; ++c) {
// mutex lock
WaitForSingleObject(mutex, INFINITE);
printf("read shared memory...c=%d\n", buf[0]);
// mutex unlock
::ReleaseMutex(mutex);
::Sleep(1000);
}
// release
::UnmapViewOfFile(buf);
::CloseHandle(shmem);
::ReleaseMutex(mutex);
return 0;
}
#include <SDKDDKVer.h>
#include <Windows.h>
#include <stdio.h>
int main(int argc, char* argv[])
{
int shmem_size = 16; // 16byte
HANDLE shmem = INVALID_HANDLE_VALUE;
HANDLE mutex = INVALID_HANDLE_VALUE;
mutex = ::CreateMutex(NULL, FALSE, "mutex_sample_name");
shmem = ::CreateFileMapping(
INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
0,
shmem_size,
"shared_memory_name"
);
char *buf = (char*)::MapViewOfFile(shmem, FILE_MAP_ALL_ACCESS, 0, 0, shmem_size);
for (unsigned int c = 0; c < 60; ++c) {
// mutex lock
WaitForSingleObject(mutex, INFINITE);
// write shared memory
memset(buf, c, shmem_size);
printf("write shared memory...c=%d\n", c);
// mutex unlock
::ReleaseMutex(mutex);
::Sleep(1000);
}
// release
::UnmapViewOfFile(buf);
::CloseHandle(shmem);
::ReleaseMutex(mutex);
return 0;
}
以上是关于c_cpp WIN32的SharedMemory示例的主要内容,如果未能解决你的问题,请参考以下文章
Win32 共享内存的包装类? (进程间通信)
c_cpp linux ln链接到win32 mklink链接
c_cpp 在win32 gui应用程序中创建一个控制台窗口
Error building Player: Win32Exception: ApplicationName='E:/adt-20140702/sdk oolszipalign.e(示
运行scrapy crawl (文件名)时显示invalid syntax和no modle 'win32api'解决方案
为何刚装的vs2010 ,调试c++时显示 "此项目4 - Debug Win32已过期"?求大神帮忙!!