Re: Ensuring that a thread doesn't own a critical section within a function




Developer wrote:

f() is responsible for destroying a second thread:

thread2_proc()
{
while(! done)
{
// ...
EnterCriticalSection(&cs);
// ...
LeaveCriticalSection(&cs);
// ...
}
}

f()
{
stop_thread2(); // set a flag that is periodically checked in
thread2_proc

WaitForSingleObject(thread2_handle, INFINITE); // wait for
thread2_proc to exit
}

This will deadlock if f() happens to own the critical section and
thread2_proc() is trying to enter it. Changing the WaitForSingleObject
timeout is not an option because f() must ensure the second thread is
truly finished.

You have a semantic problem here, and you are right that the correct
fix is to specify that 'f' cannot be called while holding the lock. The
purpose of the 'f' function is to wait for the second thread to change
state so it can't mess with things. The purpose of the lock is to
prevent the thing the second thread messes with from changing state. So
calling 'f' while holding the lock makes no semantic sense.

DS

.



Relevant Pages

  • RE: [Full-Disclosure] Vulnerability Disclosure Debate
    ... > The purpose of a lock is not security. ... The purpose of a lock is to keep unauthorized people out. ... Knowledge of limitations is just as important, ...
    (Full-Disclosure)
  • Re: Interrupting threads...
    ... The solution highly depends of the whole environment you are working on ... why should the second thread fails to ... > Lets say i have 1 mutex, ... > if it's abandoned i'll get the lock in "thread2" anyways). ...
    (microsoft.public.win32.programmer.kernel)
  • Re: lock used in thread and by event
    ... event which calls first thread's eventhandler ... then the second thread will block. ... same basic behavior and semantics as the implicit monitor lock ... lock (foo) // No blocking here ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: The dual-band if
    ... The finals have a purpose, perhaps overprotectively so, to lock down the values of certain variables. ... Shorter class names are helpful for pedagogical discourse, and maybe example snippets can dispense with 'final' and other things like complete exception handling, but conversely, emphasizing the value of 'final' inline is helpful as a sidelong hint. ...
    (comp.lang.java.programmer)
  • Re: futex question
    ... Which completely defeats the purpose of the robust list. ... lists before the point of no return, so on a failing execve() we'd have ... has an entry (lock held) in do_execveand return -EWOULDBLOCK to ...
    (Linux-Kernel)