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示例的主要内容,如果未能解决你的问题,请参考以下文章