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

  • SEARCH
  • Home
  • 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 9148387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:15:29+00:00 2026-06-17T11:15:29+00:00

I am confused as to mutex works on code segment or variables in a

  • 0

I am confused as to mutex works on code segment or variables in a code segment.

In the example below, mutex will prevent two threads trying to access mysum at the same time either from func1 or func2 or only the code-segment between mutex lock and unlock is protected

   .
   .
     pthread_mutex_t myMutex;
     int mysum;

  void func1(){
  .
  .
  .

  pthread_mutex_lock (&myMutex);
     mysum--;
     printf("Thread %ld did mysum=%d\n",id,mysum);
  pthread_mutex_unlock (&myMutex);
  .
  .
  }

  void func2(){
  .
  .
  .
  mysum++;

  pthread_mutex_lock (&myMutex);
     mysum++;
     printf("Thread %ld did mysum=%d\n",id,mysum);
  pthread_mutex_unlock (&myMutex);
  .
  .
  }

  int main (int argc, char *argv[])
  {
    pthread_mutex_init(&myMutex, NULL);
  .
  .

    pthread_create(&callThd1, &attr, func1, NULL); 

    pthread_create(&callThd2, &attr, func2, NULL); 
      pthread_create(&callThd3, &attr, func1, NULL); 

    pthread_create(&callThd4, &attr, func2, NULL); 
    pthread_create(&callThd5, &attr, func1, NULL); 

    pthread_create(&callThd6, &attr, func2, NULL); 
  .
  .


    pthread_mutex_destroy(&myMutex);
  .
  .

  }
  • 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-06-17T11:15:31+00:00Added an answer on June 17, 2026 at 11:15 am

    The mutex only provides mutual exclusion between the code sections that lie between pthread_mutex_lock() and pthread_mutex_unlock() (these are known as critical sections).

    There’s no direct link between the mutex and the shared data it’s protecting – it is up to you to ensure that you only access the shared data with the appropriate mutex locked.

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

Sidebar

Related Questions

I confused when I try to understand the code below. Can anyone explain this
Very confused about this one. This code in views.py works, but only when I'm
I'm trying to use a mutex to protect access to some hardware from multiple
I am bit confused as when try the code below I receive the desired
I am new to multi-threading programming, and confused about how Mutex works. In the
I am a little bit confused trying to implement a very simple mutex (lock)
Confused here... I have two dates which I NSLog to console and get: dates
Very confused here, trying out the yuicompressor on a simple javascript file. My js
I know the functionality of Mutex. But now I am confused about its timing.
Confused by java compilation process OK i know this: We write java source code,

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.