English 中文(简体)
确保在摧毁紧急科之前,不会坐在关键科。
原标题:Ensure no threads are waiting on a Critical Section before destroying it

我在使用关键章节时有一个问题。 我的 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);
    }
}
最佳回答

你有责任确保在销毁前不再使用欧安会。 让我们说,目前没有任何其他的路子试图进入,但有机会很快尝试。 现在,你销毁了欧安会,这一同时的路面将做些什么? 它以完全的速度删除了造成记忆存取被侵犯的关键部分?

实际解决办法取决于你目前的设计,但如果你正在摧毁read子,那么你或许会希望放弃你的要求,停止这些read子,然后等待主人处理,等待销毁。 之后,在你确信做了校对时,删除关键部分。

请注意,依赖欧安会成员价值观,如<代码>是不安全的。 LockCount <>/code>,如果你做了正确的工作,那么你甚至不需要像这样的东西。 缩略语 重要部分APIC表示,你将CRIAL_SECTION结构作为“黑箱”,使内部具体实施。

问题回答

暂无回答




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...