我正在测试我的选项是什么,以便在使用共享内存的两个进程之间进行通信,同时同步对所述共享内存的访问并防止使用互斥锁进行数据竞争。
一切都运作良好,除了表现似乎......相当差。我想知道是否是因为互斥锁的固有性能,或者是否只是我的实现是错误的。
以下是共享内存类的代码:
#ifndef SHAREDMEM_H
#define SHAREDMEM_H
#include <Windows.h>
class SharedMemMng
{
public:
SharedMemMng();
class SharedMem
{
friend class SharedMemMng;
private:
SharedMem();
void increaseCount();
int count() const;
bool isReady() const;
void setReady(bool status);
int m_counter;
bool m_isReady;
};
void setMem(SharedMem* mem)
{
m_mem = mem;
}
SharedMem* mem() const
{
return m_mem;
}
void increaseCount();
int count() const;
bool isReady() const;
void setReady(bool status);
private:
SharedMem* m_mem;
HANDLE m_hCounterMutex;
HANDLE m_hIsReadyMutex;
};
typedef SharedMemMng::SharedMem* SharedMem;
/*
* SHARED MEMORY MANAGER
*/
SharedMemMng::SharedMemMng()
{
m_hCounterMutex = CreateMutex(NULL, FALSE, "MyCounterMutex");
m_hIsReadyMutex = CreateMutex(NULL, FALSE, "MyReadyMutex");
}
void SharedMemMng::increaseCount()
{
WaitForSingleObject(m_hCounterMutex, INFINITE);
m_mem->increaseCount();
ReleaseMutex(m_hCounterMutex);
}
int SharedMemMng::count() const
{
WaitForSingleObject(m_hCounterMutex, INFINITE);
int result = m_mem->count();
ReleaseMutex(m_hCounterMutex);
return result;
}
bool SharedMemMng::isReady() const
{
WaitForSingleObject(m_hIsReadyMutex, INFINITE);
bool result = m_mem->isReady();
ReleaseMutex(m_hIsReadyMutex);
return result;
}
void SharedMemMng::setReady(bool status)
{
WaitForSingleObject(m_hIsReadyMutex, INFINITE);
m_mem->setReady(status);
ReleaseMutex(m_hIsReadyMutex);
}
/*
* SHARED MEMORY
*/
void SharedMemMng::SharedMem::increaseCount()
{
m_counter++;
}
int SharedMemMng::SharedMem::count() const
{
return m_counter;
}
bool SharedMemMng::SharedMem::isReady() const
{
return m_isReady;
}
void SharedMemMng::SharedMem::setReady(bool status)
{
m_isReady = status;
}
#endif //SHAREDMEM_H
第一个过程:
#include <Windows.h>
#include <stdio.h>
#include <ctime>
#include "sharedmem.h"
int main()
{
SharedMemMng m_sharedMemMng;
HANDLE hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL,
PAGE_READWRITE, 0,
sizeof(SharedMem),
"MySharedMem");
m_sharedMemMng.setMem((SharedMem)MapViewOfFile(hMapFile,
FILE_MAP_ALL_ACCESS,
0, 0,
sizeof(SharedMem)));
LONG test = 0;
clock_t start = clock();
for (int i = 0; i < 4000000; i++)
{
InterlockedIncrement(&test);
}
printf("result: %d (%d)\n", clock() - start, test);
clock_t start2 = clock();
for (int i = 0; i < 4000000; i++)
{
m_sharedMemMng.increaseCount();
}
printf("result2: %d\n", clock() - start2);
while (!m_sharedMemMng.isReady());
clock_t start3 = clock();
for (int i = 0; i < 2000000; i++)
{
m_sharedMemMng.increaseCount();
}
printf("result3: %d\n", clock() - start3);
while (true)
{
printf("%d\n", m_sharedMemMng.count());
Sleep(2000);
}
system("pause");
}
第二个流程:
#include <Windows.h>
#include <mutex>
#include "sharedmem.h"
int main()
{
HANDLE hMapFile = NULL;
SharedMemMng m_sharedMemMng;
while(hMapFile == NULL || m_sharedMemMng.mem() == NULL)
{
hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, "MySharedMem");
m_sharedMemMng.setMem((SharedMem) MapViewOfFile(hMapFile,
FILE_MAP_ALL_ACCESS,
0, 0,
sizeof(SharedMem)));
Sleep(200);
}
m_sharedMemMng.setReady(true);
for (int i = 0; i < 2000000; i++)
{
m_sharedMemMng.increaseCount();
}
while (true)
{
printf("%d\n", m_sharedMemMng.count());
Sleep(2000);
}
system("pause");
}
第一个测试(只有一个进程的InterlockedIncrement):38毫秒。 第二个测试(只有一个进程的increaseCount):2000毫秒。比我想象的要长,但没关系。 第三个测试:(使用2个进程的increaseCount):10000毫秒。
那么,我在实现我的互斥体时做错了什么,或者互斥量根本不是适合它的工具?