Re: PTHREAD_MUTEX_INITIALIZER



On Jun 23, 6:16 am, Anthony Williams <anthony....@xxxxxxxxx> wrote:

If the initialization is not truly static, the library must take care
of that behind the scenes, so it must be thread-safe. Yes, your code
is "guaranteed" to work, except that pthread_mutex_lock can fail for a
reason internal to the implementation, so you need to check the return
value for failure.

I wouldn't bother. There's nothing sane you can do when
pthread_mutex_lock fails. This would likely be an indication of some
kind of memory corruption or fatal error condition. You can't do
anything sane if locking a mutex fails for no application-level
reason.

Do you want to log the error? Odds are the log is protected by a
mutex.

Do you want to call 'abort'? Guess what many 'abort' implementations
do first? (Hint: They don't want more than one thread trying to
'abort' at the same time.)

Of course, 'exit' has to manipulate the list of exit handlers. Guess
what that's protected by. And the same argument about flushing stdio.
What about '_exit'? Guess what, it usually calls 'abort'.

So if your argument is that pthreads standards-compliant code can't
assume that pthread_mutex_lock won't fail for some mysterious reason,
please tell me what pthreads standards-compliant functions you can
reasonably expect to work after such a failure. Good luck with that.

DS
.



Relevant Pages

  • Re: pragma pack and bitfields
    ... > abort(); ... The reason has surely to do with the underlying integer type of ... and fails the comparison with two. ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Concurrency and delegates
    ... Is there some reason I'm not ... I'm not really clear on why that's considered thread-safe, but it's apparently common enough that the question of the assignment to a local variable being optimized out has come up before. ... Unfortunately, I don't recall any thread that ever provided any conclusive statements one way or the other on either issue (whether the technique works, and whether it can be accidently defeated by the optimizer). ... The only discrepancy would be the use of "this" as opposed to a dedicated object instance for locking. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: visibility on array elements
    ... AtomicLongArray and go with a long(the reason is ... when accessing the elements of the array: ... synchronize on the individual elements, ... itself guarantee "thread-safe behavior." ...
    (comp.lang.java.programmer)
  • Re: Thread Abort
    ... Actualy I know about this API, But I don't know can I call it from my C# ... BOOL TerminateThread( ... and I can't wait until Abort finish his job, ... There are a lot of reason in our ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Coding inside the debugger
    ... but why isn't the test written so that the reason it fails is ... If something fails in tests like these, ... public void testCreateWithDefaultthrows Exception { ... Ghostworld world = worldWithClass; ...
    (comp.object)