Is TMutex re-entrant in Delphi?
Asked Answered
S

1

5

I'm creating my mutex:

 FMutex := TMutex.Create(nil, False, 'SomeDumbText');

and use it in a method which calls another method using the same created mutex:

procedure a;
begin
  FMutex.Acquire;
  try
    //do some work here and maybe call b
  finally
    FMutex.Release;
  end;
end;

procedure b;
begin
  FMutex.Acquire;
  try
    //do some work here
  finally
    FMutex.Release;
  end;
end;

It is safe to have nested mutex?

Sommer answered 14/12, 2018 at 10:40 Comment(0)
K
12

TMutex is implemented over the underlying platform object. On Windows that is the mutex object. On the other platforms that is the pthread mutex.

Your question is whether or not TMutex is re-entrant. In turn, the answer depends on whether or not the underlying platform object is re-entrant. The Windows mutex is always re-entrant, the pthread mutex is optionally re-entrant, and the Delphi TMutex code chooses to use it in re-entrant mode, by calling pthread_mutexattr_settype(Attr, PTHREAD_MUTEX_RECURSIVE).

So, the answer to your question is that TMutex is re-entrant.

Karlsruhe answered 14/12, 2018 at 11:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.