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 3230916
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:59:00+00:00 2026-05-17T16:59:00+00:00

I am writing a program with a few critical sections. The thing is I

  • 0

I am writing a program with a few critical sections. The thing is I need to check the value of a mutex in an if statement.

I would like to do something like this:

if pthread_mutex(&mutex) == 0 // locked 
  // Do something
else if pthread_mutex(&mutex) == 1 // unlocked 
 // Do something else

Is this possible?

  • 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-17T16:59:01+00:00Added an answer on May 17, 2026 at 4:59 pm

    You want pthread_mutex_trylock().

    From that link:

    The pthread_mutex_trylock() function shall be equivalent to pthread_mutex_lock(), except that if the mutex object referenced by mutex is currently locked (by any thread, including the current thread), the call shall return immediately.

    …

    Return values

    …

    The pthread_mutex_trylock() function shall return zero if a lock on the mutex object referenced by mutex is acquired. Otherwise, an error number is returned to indicate the error

    So your code would go like this:

    pthread_mutex_t *m = /* ... */;
    
    if (pthread_mutex_trylock(m) == 0)
    {
        /* Success!  This thread now owns the lock. */
    }
    else
    {
        /* Fail!  This thread doesn't own the lock.  Do something else... */
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a program in C# that will need to store a few Data
I would like to distribute a program I'm halfway done writing but I'm having
I have a program using android and I would like a few tips on
I am writing a program that is cross platform. There are a few spots
I'm writing a program consisting of dynamically created panels that each have a few
I am trying to speeden up my Matlab program by writing a few functions
I am writing code in c++. I need to support few basic data types
I'm writing a program using jdbc that will be an interface to database(smth like
I'm writing a Perl program that will take a few command-line arguments (they'll actually
I am writing a WIN32 DirectX 9 program in which I load a few

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.