基于C++的進程間共享內(nèi)存通信
1、Writer端
參照上篇共享內(nèi)存通信創(chuàng)建過程,內(nèi)存寫入端代碼如下所示,其中寫入端與讀取端的共享內(nèi)存名稱、互斥量名稱要保持唯一一致:
1 // 共享內(nèi)存數(shù)據(jù)結(jié)構(gòu),并保持單字節(jié)對齊 2 #pragma pack(push, 1) 3 struct SharedData { 4 int count; 5 char buffer[256]; 6 }; 7 #pragma pack(pop) 8 9 int main() 10 { 11 int a = sizeof(SharedData); 12 std::cout << "the value of a is:" << a << std::endl; 13 // 創(chuàng)建或打開共享內(nèi)存 14 HANDLE hMapFile = CreateFileMapping( 15 INVALID_HANDLE_VALUE, // 使用物理內(nèi)存而非文件 16 NULL, // 默認(rèn)安全屬性 17 PAGE_READWRITE, // 可讀可寫 18 0, // 內(nèi)存大小高位 19 sizeof(SharedData), // 內(nèi)存大小低位 20 L"MySharedMemory" // 共享內(nèi)存名稱(需唯一) 21 ); 22 23 if (hMapFile == NULL) { 24 std::cerr << "CreateFileMapping failed: " << GetLastError() << std::endl; 25 return 1; 26 } 27 28 // 將共享內(nèi)存映射到當(dāng)前進程地址空間 29 SharedData* pSharedData = (SharedData*)MapViewOfFile( 30 hMapFile, // 共享內(nèi)存句柄 31 FILE_MAP_ALL_ACCESS, // 可讀可寫 32 0, 0, // 偏移量 33 sizeof(SharedData) // 映射大小 34 ); 35 36 if (pSharedData == NULL) { 37 std::cerr << "MapViewOfFile failed: " << GetLastError() << std::endl; 38 CloseHandle(hMapFile); 39 return 1; 40 } 41 42 // 創(chuàng)建互斥量(用于同步) 43 HANDLE hMutex = CreateMutex( 44 NULL, // 默認(rèn)安全屬性 45 FALSE, // 初始狀態(tài)為未鎖定 46 L"MyMutex" // 互斥量名稱(需唯一) 47 ); 48 49 if (hMutex == NULL) { 50 std::cerr << "CreateMutex failed: " << GetLastError() << std::endl; 51 UnmapViewOfFile(pSharedData); 52 CloseHandle(hMapFile); 53 return 1; 54 } 55 56 std::cout << "Writer started. Press Ctrl+C to exit..." << std::endl; 57 int count = 0; 58 59 while (true) { 60 // 等待互斥量(加鎖) 61 WaitForSingleObject(hMutex, INFINITE); 62 63 // 寫入數(shù)據(jù) 64 pSharedData->count = count; 65 sprintf_s(pSharedData->buffer, "Hello from writer %d", count); 66 std::cout << "Writer: Wrote " << count << std::endl; 67 count++; 68 69 // 釋放互斥量(解鎖) 70 ReleaseMutex(hMutex); 71 72 Sleep(100); // 等待100毫秒 73 } 74 75 // 清理(通常不會執(zhí)行到這里) 76 UnmapViewOfFile(pSharedData); 77 CloseHandle(hMapFile); 78 CloseHandle(hMutex); 79 80 return 0; 81 }
2、Reader端
#include <iostream> #include <Windows.h> // 共享內(nèi)存數(shù)據(jù)結(jié)構(gòu) struct SharedData { int count; char buffer[256]; }; int main() { // 打開共享內(nèi)存 HANDLE hMapFile = OpenFileMapping( FILE_MAP_ALL_ACCESS, // 可讀可寫 FALSE, // 不繼承句柄 L"MySharedMemory" // 共享內(nèi)存名稱 ); if (hMapFile == NULL) { std::cerr << "OpenFileMapping failed: " << GetLastError() << std::endl; return 1; } // 將共享內(nèi)存映射到當(dāng)前進程地址空間 SharedData* pSharedData = (SharedData*)MapViewOfFile( hMapFile, // 共享內(nèi)存句柄 FILE_MAP_ALL_ACCESS, // 可讀可寫 0, 0, // 偏移量 sizeof(SharedData) // 映射大小 ); if (pSharedData == NULL) { std::cerr << "MapViewOfFile failed: " << GetLastError() << std::endl; CloseHandle(hMapFile); return 1; } // 打開互斥量 HANDLE hMutex = OpenMutex( MUTEX_ALL_ACCESS, // 訪問權(quán)限 FALSE, // 不繼承句柄 L"MyMutex" // 互斥量名稱 ); if (hMutex == NULL) { std::cerr << "OpenMutex failed: " << GetLastError() << std::endl; UnmapViewOfFile(pSharedData); CloseHandle(hMapFile); return 1; } std::cout << "Reader started. Press Ctrl+C to exit..." << std::endl; int count = -1; while (true) { // 等待互斥量(加鎖) WaitForSingleObject(hMutex, INFINITE); //讀取到序號相同的數(shù)據(jù)進行丟棄 if (count == pSharedData->count) { ReleaseMutex(hMutex); continue; } // 讀取數(shù)據(jù) std::cout << "Reader: Read " << pSharedData->count << ", " << pSharedData->buffer << std::endl; count = pSharedData->count; // 釋放互斥量(解鎖) ReleaseMutex(hMutex); Sleep(100); // 等待100毫秒 } // 清理 UnmapViewOfFile(pSharedData); CloseHandle(hMapFile); CloseHandle(hMutex); return 0; }
3、效果演示

記性太差,需要這么記下來

浙公網(wǎng)安備 33010602011771號