线程同步问题

ppyy 2003-12-22 01:24:11
我们大家都知道,临界段代码只能在进程内同步。。。。


现在有一个DLL,定义了共享数据段,这个共享数据段里有一个数据结构被各进程共享。。。。
这个DLL被很多EXE调用。。。。

这个DLL导出一个函数:ModiValue,功能就是修改这个共享的数据结构,,,

问题是:DLL被映射到了各个进程里去了,能否用临界段对这个共享数据结构实行同步?
...全文
17 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
lygfqy 2003-12-22
  • 打赏
  • 举报
回复
不妨创建共享内存文件来实现呢需要的功能
hellion 2003-12-22
  • 打赏
  • 举报
回复
应该是不行的
ppyy 2003-12-22
  • 打赏
  • 举报
回复
UP一下
ppyy 2003-12-22
  • 打赏
  • 举报
回复
我是在一本书上看到有人这么做。。。。
所以特意上来问一问。
召唤权威的答案
ppyy 2003-12-22
  • 打赏
  • 举报
回复
sinhighly(非典型程序员):

兄弟谢谢你,,,
但是请你看清楚我的问题。
sinhighly 2003-12-22
  • 打赏
  • 举报
回复
总而言之,就是获取或修改共享数据段结构内容前先获取控制权mutex
sinhighly 2003-12-22
  • 打赏
  • 举报
回复
easy
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch( ul_reason_for_call )
{
case DLL_PROCESS_ATTACH:
// Create a mutex with no initial owner.
hMutex = CreateMutex(
NULL, // no security attributes
FALSE, // initially not owned
"MutexToProtectMMIStatus"); // name of mutex

if (hMutex == NULL)
{
//ERROR to create mutex
MessageBox(NULL,"error to create mutex","Error",MB_OK);
//exit
return FALSE;
}

//One more dll refed
g_s32DllRef ++;
break;
case DLL_PROCESS_DETACH:
// close handle of the mutex
CloseHandle(hMutex);
//One dll nrefed
g_s32DllRef --;
break;
default: break;
}
return TRUE;
}
STATUS GetMMIStatus(MMIStatus &mmiStatusStruRef)
{
//wait mutex
if (WaitForMutex(hMutex) == OK)
{
//get the mutex
//get MMI status
memcpy(&mmiStatusStruRef,&g_mmiStatusStru,sizeof(MMIStatus));
// dstatus = MMIStatues;
//release mutex
ReleaseMutex(hMutex);
return OK;
}
else
{
//fail to get mutex
return ERROR;
}

}
STATUS WaitForMutex(HANDLE hMutex)
{
DWORD dwwaitresult;

// Request ownership of mutex.
// wait the mutex for 5 second
dwwaitresult = WaitForSingleObject(
hMutex, // handle to mutex
5000L); // five-second time-out interval

switch (dwwaitresult)
{
// The thread got mutex ownership.
case WAIT_OBJECT_0:
//get the mutex
/* __try
{
// Write to the database.
}

__finally
{
// Release ownership of the mutex object.
if (! ReleaseMutex(hMutex))
{
// Deal with error.
}

}
*/ return OK;
// break;

// Cannot get mutex ownership due to time-out.
case WAIT_TIMEOUT:
//Faile to get mutex (reason is timeout)
// return FALSE;

// Got ownership of the abandoned mutex object.
case WAIT_ABANDONED:
//Faile to get mutex(can not get)
// return FALSE;
default:break;//return FALSE;
}

// return TRUE;
return ERROR;
}
ok了
topwork 2003-12-22
  • 打赏
  • 举报
回复
好像不太行;
如果这种同步请考虑用Event或Mutex内核对象完成吧;
ppyy 2003-12-22
  • 打赏
  • 举报
回复
我觉得应该不行,可是看到一本书上说可以,,,,
特意来问一下

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧