-3

On the one hand I want to lock some critical section with 'Lock()', but on the other hand I want to let possibility of suspending a thread at some button's click (I know that the 'Suspend' function should not be used, but I really have no choice).

Possibly that other threads will want to enter to this critical section. How can I prevent the DEADLOCK?

If the 'Suspend' function was throwing exception (like the 'Abort()' function), there was no problem. The monitor was released in the 'finally' statement...

For example:

static List<Thread> threadList = new List<Thread>();
public static List<Thread> ThreadList
{
    get { lock (_locker) { return SaveLists.threadList; } }
    set { lock (_locker) { SaveLists.threadList = value; } }
}

If I'll suspend the thread that locked the object, and after thate an another thread will want to set/get the list =>DEADLOCK.

4

1 回答 1

1

原来解决方法太简单了!我很惊讶自己以前怎么没想到……

负责挂起其他线程的线程应该先锁定临界区,从而有效保证其他线程不会在里面。

我知道我不应该使用“暂停”功能,但我被要求使用它,我别无选择。

太糟糕了,我的分数没有得到适当的回应,反而下降了,答案是这样的:“你永远不能使用这个功能”,尽管我写了我必须使用它,尽管有危险,我知道一般来说它的使用是被禁止的。

我很失望,我认为他不适合这个论坛。

于 2013-08-31T22:45:20.867 回答