Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7489065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:11:44+00:00 2026-05-29T15:11:44+00:00

pthread_mutex_trylock detects deadlocks, doesn’t block, then why would you even need pthread_mutex_lock ? Perhaps

  • 0

pthread_mutex_trylock detects deadlocks, doesn’t block, then why would you even “need” pthread_mutex_lock?

Perhaps when you deliberately want the thread to block? But in that case it may result in a deadlock?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T15:11:44+00:00Added an answer on May 29, 2026 at 3:11 pm

    pthread_mutex_trylock does not detect deadlocks.

    You can use it to avoid deadlocks but you have to do that by wrapping your own code around it, effectively multiple calls to pthread_mutex_trylock in a loop with a time-out, after which your thread releases all its resources.

    In any case, you can avoid deadlocks even with pthread_mutex_lock if you just follow the simple rule that all threads allocate resources in the same order.

    You use pthread_mutex_lock if you just want to efficiently wait until the resource is available, without having to spin on the mutex, something which is often very inefficient. Properly designed multi-threaded applications have no need for the pthread_mutex_trylock variant.

    Locks should only be held for the absolute minimum time to do the work and, if that’s too long, you can generally redesign things so the lock time is less (such as by using the mutex to only copy data to a thread’s local data areas, and having the long-running bit work on that after the mutex is released).

    The pseudo-code:

    while not pthread_mutex_trylock:
        yield
    

    will continue to run your thread, waiting for the lock to be available, especially since there is no pthread_yield() in POSIX threads (though it’s sometimes provided as a non-portable extension).

    That means, at worst, the code segment above won’t even be able to portably yield the CPU, therefore chewing up the rest of it’s quantum every time through the scheduler cycle.

    And at best, it will still activate the thread once per scheduler cycle just to see if the mutex can be obtained.

    Whereas:

    pthread_mutex_lock
    

    will most likely totally pause your thread until the lock is made available, since it will move it to a waiting queue until the current lock holder releases the mutex.

    That’s probably the major reason why you should prefer pthread_mutex_lock to pthread_mutex_trylock.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that we can use pthread_mutex_init and pthread_mutex_lock to implement thread mutual exclusion.
I would like to be able to force a context switch from one thread
pthread_create returns the value 251 without creating the thread. Does anyone know what the
From pthread_join() man page : When a joinable thread terminates, its memory resources (thread
I presumed that a pthread_t remains constant - for a given thread - for
The function header for pthread_create looks like this: int pthread_create(pthread_t * thread, const pthread_attr_t
I am having a problem with mutexes (pthread_mutex on Linux) where if a thread
I have two threads. First one is something like this: while(1) { pthread_mutex_lock(&mutex); //DO
Assume that the following code is being executed by 10 threads. pthread_mutex_lock(&lock) Some trivial
can you explain me why the following use of pthread_join doesn't work?it blocks my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.