window共享内存

#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <ctime>

int semaphoreImpl() {
	srand((unsigned)time(NULL));

	// 创建或打开共享内存
	HANDLE fileMapping = CreateFileMapping(
		INVALID_HANDLE_VALUE,
		NULL,
		PAGE_READWRITE,
		0,
		sizeof(int),
		L"SharedMemoryForInteger");

	if (fileMapping == NULL) {
		std::cerr << "Could not create file mapping object: " << GetLastError() << std::endl;
		return 1;
	}

	// 映射共享内存区域
	int* sharedInt = (int*)MapViewOfFile(
		fileMapping,
		FILE_MAP_ALL_ACCESS,
		0,
		0,
		sizeof(int));

	if (sharedInt == NULL) {
		std::cerr << "Could not map view of file: " << GetLastError() << std::endl;
		CloseHandle(fileMapping);
		return 1;
	}

	// 初始化共享整数
	*sharedInt = 0;

	// 创建或打开信号量
	HANDLE semaphore = CreateSemaphore(
		NULL,
		1,
		1,
		L"MySemaphore");

	if (semaphore == NULL) {
		std::cerr << "CreateSemaphore error: " << GetLastError() << std::endl;
		UnmapViewOfFile(sharedInt);
		CloseHandle(fileMapping);
		return 1;
	}

	while (*sharedInt < 10) {
		WaitForSingleObject(semaphore, INFINITE); // 等待信号量, 即P操作

		std::cout << "Incrementor1: Current value = " << *sharedInt << ". Incrementing..." << std::endl;
		(*sharedInt)++;
		Sleep(rand() % 1000 + 500); // 随机延时500到1500毫秒

		ReleaseSemaphore(semaphore, 1, NULL); // 释放信号量,即V操作
	}

	// 清理
	UnmapViewOfFile(sharedInt);
	CloseHandle(fileMapping);
	CloseHandle(semaphore);

	return 0;
}


int main() {

   semaphoreImpl();

   // 等待关闭应用程序
   getchar();

   return 0;
}
#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <ctime>

int semaphoreImpl() {
	srand((unsigned)time(NULL));

	// 创建或打开共享内存
	HANDLE fileMapping = CreateFileMapping(
		INVALID_HANDLE_VALUE,
		NULL,
		PAGE_READWRITE,
		0,
		sizeof(int),
		L"SharedMemoryForInteger");

	if (fileMapping == NULL) {
		std::cerr << "Could not create file mapping object: " << GetLastError() << std::endl;
		return 1;
	}

	// 映射共享内存区域
	int* sharedInt = (int*)MapViewOfFile(
		fileMapping,
		FILE_MAP_ALL_ACCESS,
		0,
		0,
		sizeof(int));

	if (sharedInt == NULL) {
		std::cerr << "Could not map view of file: " << GetLastError() << std::endl;
		CloseHandle(fileMapping);
		return 1;
	}

	// 初始化共享整数
	*sharedInt = 0;

	// 创建或打开信号量
	HANDLE semaphore = CreateSemaphore(
		NULL,
		1,
		1,
		L"MySemaphore");

	if (semaphore == NULL) {
		std::cerr << "CreateSemaphore error: " << GetLastError() << std::endl;
		UnmapViewOfFile(sharedInt);
		CloseHandle(fileMapping);
		return 1;
	}

	while (*sharedInt < 10) {
		WaitForSingleObject(semaphore, INFINITE); // 等待信号量, 即P操作

		std::cout << "Incrementor1: Current value = " << *sharedInt << ". Incrementing..." << std::endl;
		(*sharedInt)++;
		Sleep(rand() % 1000 + 500); // 随机延时500到1500毫秒

		ReleaseSemaphore(semaphore, 1, NULL); // 释放信号量,即V操作
	}

	// 清理
	UnmapViewOfFile(sharedInt);
	CloseHandle(fileMapping);
	CloseHandle(semaphore);

	return 0;
}


int main() {

   semaphoreImpl();

   // 等待关闭应用程序
   getchar();

   return 0;
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值