我在使用关键章节时有一个问题。 我的 app子很多,即60条,所有人都需要获得全球资源。 因此,我用一个关键部分保护这一资源。 然而,在我的申请被关闭时,我完全在行动期间这样做,我触发了quit,然后摧毁了关键部分。
问题在于,如果其中一些read子在离去时等待关键部分,从而被阻挡不清。
我在窗户周围写了一幅有初步旗帜的画面,当造物时,我定真是这样,当我即将离开十字架时,我会冒犯(两个案件都是在十字架中确定的)。 这一旗帜在试着进入十字架之前经过检查,如果国旗不实,则绕过请求。 国旗还检查了成功进入十字军的路口,如果该国旗是假的,它就会立即离开。
在解除戒律之前,我要做的是打上旗帜,然后等到任何等待的read子:被允许进入十字架;看到最初的旗帜是假的;然后离开十字架(应为每一条read子做一个非常快的运行)。
我检查了坐视器在CRITICAL-SECTION 构体内检查洛克科伦,并等待点击(在XP中,)。 LockCount - (RecursionCount-1)
; 2003年,服务器及以上,锁编号为((-1)-(LockCount)” >> 2
,然后销毁关键部分。
但是,Im认为,当仍然有一线(仅一条路,从来就更不用说)等待进入十字架时,洛克科特就已到达零,也就是说,如果我删除了当时的位置,则另一条路子后来从坐在十字架上,造成坠毁。 到那时为止,已经销毁了该物品。
If I keep my own internal lock count of threads waiting for access, I have the correct count; however this isn t ideal as I have to increment this count outside of the crit, meaning that value isn t protected and therefore can t be entirely relied upon at any one time.
是否有任何人知道,为什么从1岁起就将洛克比拉 in锁中去掉? 如果我使用我自己的锁定,那么,检查一下最后一个胎面(在我销毁 cr之前)之后的化学、生物、化学、生物、化学、生物、化学、生物、化学、生物、化学、生物、化学、生物、生物、化学、生物、化学、生物、化学、生物、化学、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、生物、
或者,除了一个关键部分外,我是否能够更好地保护全球资源。
这是我的总结:
typedef struct MY_CRIT {
BOOL Initialised;
CRITICAL_SECTION Crit;
int MyLockCount;
}
这里,我的职责是:
BOOL InitCrit( MY_CRIT *pCrit )
{
if (pCrit)
{
InitializeCriticalSection( &pCrit->Crit );
pCrit->Initialised = TRUE;
pCrit->MyLockCount = 0;
return TRUE;
}
// else invalid pointer
else
return FALSE;
}
这是我的结晶功能:
BOOL EnterCrit( MY_CRIT *pCrit )
{
// if pointer valid, and the crit is initialised
if (pCrit && pCrit->Initialised)
{
pCrit->MyLockCount++;
EnterCriticalSection( &pCrit->Crit );
pCrit->MyLockCount--;
// if still initialised
if (pCrit->Initialised)
{
return TRUE;
}
// else someone s trying to close this crit - jump out now!
else
{
LeaveCriticalSection( &pCrit->Crit );
return FALSE;
}
}
else // crit pointer is null
return FALSE;
}
我的“自由文化”总结职能:
void FreeCrit( MY_CRIT *pCrit )
{
LONG WaitingCount = 0;
if (pCrit && (pCrit->Initialised))
{
// set Initialised to FALSE to stop any more threads trying to get in from now on:
EnterCriticalSection( &pCrit->Crit );
pCrit->Initialised = FALSE;
LeaveCriticalSection( &pCrit->Crit );
// loop until all waiting threads have gained access and finished:
do {
EnterCriticalSection( &pCrit->Crit );
// check if any threads are still waiting to enter:
// Windows XP and below:
if (IsWindowsXPOrBelow())
{
if ((pCrit->Crit.LockCount > 0) && ((pCrit->Crit.RecursionCount - 1) >= 0))
WaitingCount = pCrit->Crit.LockCount - (pCrit->Crit.RecursionCount - 1);
else
WaitingCount = 0;
}
// Windows 2003 Server and above:
else
{
WaitingCount = ((-1) - (pCrit->Crit.LockCount)) >> 2;
}
// hack: if our own lock count is higher, use that:
WaitingCount = max( WaitingCount, pCrit->MyLockCount );
// if some threads are still waiting, leave the crit and sleep a bit, to give them a chance to enter & exit:
if (WaitingCount > 0)
{
LeaveCriticalSection( &pCrit->Crit );
// don t hog the processor:
Sleep( 1 );
}
// when no other threads are waiting to enter, we can safely delete the crit (and leave the loop):
else
{
DeleteCriticalSection( &pCrit->Crit );
}
} while (WaitingCount > 0);
}
}