问题是这样的,目前我有三个程序,三块共享内存,每个程序都可以访问这三快共享内存(我现在是在每个程序中用CreateMutex函数声明了一个锁,用CreateFileMapping和MapViewOfFile函数分别定义三个共享内存)当我使用某一块内存时得先调用WaitForSingleObject函数来确定数据的有效性,可是每次调用WaitForSingleObject函数对一块内存进行操作时其他两块也处于锁定状态了,我想每次操作那块就对那块锁定,想请教您这个问题如何解决呀,不知道说明白没有
解决方案 »
- 如何将数据写入到Sql Server2000中的binary类型的字段中。
- 流程图制作软件哪款比较好
- 面试题目:如何启动其他进程,写出你所知道的方式?
- [难题]已知一个进程ID\API函数名\DLL名\求此API函数在此进程中的入口地址
- 对话框上默认的OK和CANCEL按钮怎么重新加上去啊?
- 请问:如何给vc的exe文件打包?
- 是不是和串口有关?
- 我要下载文件到嵌入设备,用TFTP方式,请问在VC中怎样实现客户端程序.
- 用CList如何存储多种不同类型的数据?
- 关于菜单命令的一个问题,急!(我是新手,只有30分,大家帮忙)
- 如何释放socket的系统缓存
- 郁闷!!为什么我在对话框调用InvalidateRect后在OnPaint里用BitBlt拷出来的内存DC没有显示?
m_hDataLock = ::CreateMutex(NULL, false, lockname);
if (INVALID_HANDLE_VALUE == m_hDataLock || m_hDataLock==NULL)
{
AfxMessageBox("句柄分配错误", MB_ICONERROR);
return false;
}//Creatmem("ShareMem Sample App");
for(i=1;i<=MAX_BUFF_NUM;i++)
{
name[0]='\0';
name[1]='\0';
sprintf(name,"%d",i);
// AfxMessageBox(name);
m_hFileMapping = ::CreateFileMapping(
(HANDLE)0xFFFFFFFF, // File handle
NULL, // Security attributes
PAGE_READWRITE, // Protection
0, // Size - high 32 bits
sizeof(struct mem_struct), // Size - low 32 bits
name); // Name
if(m_hFileMapping ==NULL)
{
return false;
}
m_buf[i]= (struct mem_struct *)::MapViewOfFile(
m_hFileMapping, // File mapping object
FILE_MAP_ALL_ACCESS, // Read/Write
0, // Offset - high 32 bits
0, // Offset - low 32 bits
sizeof(struct mem_struct)); // Map the whole thing
}
for(i=1;i <=MAX_BUFF_NUM;i++)
{
sprintf(lockname,"lock%d",i);
m_hDataLock[i] = ::CreateMutex(NULL, false, lockname[i]);
if (INVALID_HANDLE_VALUE == m_hDataLock[i] ¦ ¦ m_hDataLock[i]==NULL)
{
AfxMessageBox("句柄分配错误", MB_ICONERROR);
return false;
} name[0]='\0';
name[1]='\0';
sprintf(name,"%d",i);
// AfxMessageBox(name);
m_hFileMapping[i] = ::CreateFileMapping(
(HANDLE)0xFFFFFFFF, // File handle
NULL, // Security attributes
PAGE_READWRITE, // Protection
0, // Size - high 32 bits
sizeof(struct mem_struct), // Size - low 32 bits
name); // Name
if(m_hFileMapping[i] ==NULL)
{
return false;
} m_buf[i]= (struct mem_struct *)::MapViewOfFile(
m_hFileMapping[i], // File mapping object
FILE_MAP_ALL_ACCESS, // Read/Write
0, // Offset - high 32 bits
0, // Offset - low 32 bits
sizeof(struct mem_struct)); // Map the whole thing
}