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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:02:50+00:00 2026-05-22T22:02:50+00:00

If I lock the same mutex in two different places in my function, and

  • 0

If I lock the same mutex in two different places in my function, and a context switch occurs when one thread is in one of them, and the second thread gets to the other one, will it be blocked?

I’ll try to give a simple example of what I mean, maybe it will be clearer.
Say I have the following code in a file test.c

int globalVar = 0;    

void testMutex(pthread_mutex_t myMutex) {
    pthread_mutex_lock(&myMutex);
    globalVar++;
    pthread_mutex_unlock(&myMutex);

    printf("%s \n", "Doing some other stuff here");

    pthread_mutex_lock(&myMutex);
    globalVar--;
    pthread_mutex_unlock(&myMutex);
}

and in a different file main.c, I have a main function which create two threads, thread1 and thread2, both running testMutex function.
thread1 is executed first, and while in the second part of the function (the — part, after printf), before mutex is unlocked, thread2 start running, from the function beginning.
will thread2 be able to execute globalVar++, or will it stay blocked, waiting for the mutex to be unlocked?

Thanks in advance!

  • 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-22T22:02:51+00:00Added an answer on May 22, 2026 at 10:02 pm

    The short answer is “yes”.

    The pthread_mutex_lock documentation makes this pretty clear:

    The mutex object referenced by mutex shall be locked by calling pthread_mutex_lock(). If the mutex is already locked, the calling thread shall block until the mutex becomes available. This operation shall return with the mutex object referenced by mutex in the locked state with the calling thread as its owner.

    In other words, upon return from pthread_mutex_lock, the mutex is “owned” by the thread. The system guarantees that at most one thread can “own” a single mutex at any time.

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

Sidebar

Related Questions

Suppose I have code something like this: #include boost/thread/mutex.hpp using boost::mutex; typedef mutex::scoped_lock lock;
I want to lock other threads when one thread is running. It is possible
I am trying to synchronize two thread (working on the same C++ map) using
I understand the main function of the lock key word from MSDN lock Statement
If you have two threads within an application, and you don't want them to
I have one main thread that does some rather CPU intensive operation. The thread
I haven't had any issues using the same lock for multiple methods so far,
How do I lock compiled Java classes to prevent decompilation? I know this must
The scroll lock button seems to be a reminder of the good old green
I have to lock user accounts in Active Directory programmatically in C#. Unfortunately it

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.