gpt4 book ai didi

multithreading - 如果2个线程执行EnterCriticalSection而线程1执行DeleteCriticalSection会发生什么

转载 作者:行者123 更新时间:2023-12-04 06:39:47 24 4
gpt4 key购买 nike

我正在寻找程序中可能出现的僵局,并怀疑以下情况。
如果在输入后立即有2个线程同时调用EnterCriticalSection和线程#1调用DeleteCriticalSection会发生什么,仍在EnterCriticalSection调用中的线程#2会发生什么?

谢谢。

最佳答案

What happen if 2 threads at the same time calls EnterCriticalSection and thread #1 calls DeleteCriticalSection right after entering, what happen to thread #2 which is still in EnterCriticalSection call?



两个线程不能同时进入关键部分。那会破坏关键部分的目的。线程#1首先进入关键部分,或者线程#2首先进入关键部分。您在这里有两种可能的交错。

假设交错是这样的:
    Thread 1               Thread 2    --------               --------       |                      |       |                      |    EnterCS()                 |    Lock Taken                |       |                      |       |                   EnterCS()       |                   Blocked       |                      |       |                      |    DeleteCS()                |       |                      |       |                     ???       |      ...

In this case, the state of thread #2 is undefined according to MSDN:

DeleteCriticalSection function

Remarks

Deleting a critical section object releases all system resources used by the object.

After a critical section object has been deleted, do not reference the object in any function that operates on critical sections (such as EnterCriticalSection, TryEnterCriticalSection, and LeaveCriticalSection) other than InitializeCriticalSection and InitializeCriticalSectionAndSpinCount. If you attempt to do so, memory corruption and other unexpected errors can occur.

If a critical section is deleted while it is still owned, the state of the threads waiting for ownership of the deleted critical section is undefined.

So if you're unlucky enough for your two threads to encounter the above interleaving, then the operating system gives you no guarantee that your program will continue to work as expected. That can include deadlocking Thread #2, for example.

But if the interleaving is this:

    Thread 1               Thread 2    --------               --------       |                      |       |                      |       |                   EnterCS()       |                   Lock Taken       |                      |    EnterCS()                 |    Blocked                   |       |                      |       |                      |       |                   ExitCS()       |                   Lock Released       |                      |    Unblocked                 |    LockTaken                 |       |                      |    DeleteCS()                |       |                      |       |                      |      ...                    ...

Then obviously, since Thread #1 is blocked, it can't delete the critical section, until Thread #2 leaves the critical section. Then assuming no other threads enter the critical section, Thread #1 will be able to delete it with no problems.

The scenario that you propose is essentially a race condition. Depending on the timing of the threads, it may work just fine or cause unpredictable problems. In this case, you have to restructure your code such that the destruction of the critical section happens after all interested threads have released the critical section.

In this two-thread scenario, one way to fix it is to have Thread #1 leave the critical section and wait for all the other threads to finish first before deleting the critical section. Something like this, for example:

// Pseudocode for exposition
void Thread1()
{
EnterCS();
// Do stuff
ExitCS();
WaitForThread2();
DeleteCS();
}

void Thread2()
{
EnterCS();
// Do stuff
ExitCS();
}

现在,两种可能的交错看起来像这样:

线程#2首先获取锁:。线程#1首先获取锁:

线程1线程2。线程1线程2
-------- -------- -------- --------
| | 。 | |
| EnterCS()。 EnterCS()|
|锁已采取。锁
| | 。 | |
EnterCS()| 。//做EnterCS()
阻止//做事。 |受阻
| | 。 | |
| | 。 ExitCS()|
| ExitCS()。锁已释放|
|锁已释放。 | |
| | 。 |畅通
畅通无阻。 |锁定
锁。 | |
| | 。 |//做东西
//做事| 。 | |
| | 。 | ExitCs()
ExitCS()| 。 |锁已释放
锁已释放| 。 | |
| | 。 | |
| | 。 | |
WaitForThread2()-+。 WaitForThread2()-+
| 。 |
DeleteCS()。 DeleteCS()
| 。 |
| 。 |
完毕 。完毕
WaitForThread2()的确切实现将取决于程序的性质,但肯定会涉及 WaitForSingleObject() 或其任何亲戚。

关于multithreading - 如果2个线程执行EnterCriticalSection而线程1执行DeleteCriticalSection会发生什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12774425/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com