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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:51:35+00:00 2026-05-19T17:51:35+00:00

Sorry just found this code here – http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html and the mutex was explained with

  • 0

Sorry just found this code here – http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html and the mutex was explained with this code, but it has gone a little over my head. I understand the function of the mutex, and that it protects a shared variable during its critical section. The specifics here are confusing me though! By my understanding, we are creating a new thread with pthread_create, it’s running the functionC process, which increments a counter. The counter is the protected variable, and, since both functions are running concurrently, the counter would return the wrong value if it was not protected by the mutex.

Is this correct/close ? Thanks a lot :).

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void *functionC();
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
int  counter = 0;

main()
{
   int rc1, rc2;
   pthread_t thread1, thread2;

   /* Create independent threads each of which will execute functionC */

   if( (rc1=pthread_create( &thread1, NULL, &functionC, NULL)) )
   {
      printf("Thread creation failed: %d\n", rc1);
   }

   if( (rc2=pthread_create( &thread2, NULL, &functionC, NULL)) )
   {
      printf("Thread creation failed: %d\n", rc2);
   }

   /* Wait till threads are complete before main continues. Unless we  */
   /* wait we run the risk of executing an exit which will terminate   */
   /* the process and all threads before the threads have completed.   */

   pthread_join( thread1, NULL);
   pthread_join( thread2, NULL); 

   exit(0);
}

void *functionC()
{
   pthread_mutex_lock( &mutex1 );
   counter++;
   printf("Counter value: %d\n",counter);
   pthread_mutex_unlock( &mutex1 );
}
  • 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-19T17:51:36+00:00Added an answer on May 19, 2026 at 5:51 pm

    If you don’t have the mutex in place, this is what might happen:

    // initialization
    counter = 0;
    
    // thread 1 runs:
    counter++;
    
    // context switch
    // thread 2 runs:
    counter++;
    
    // context switch
    // thread 1 runs and printf "Counter value: 2"
    printf("Counter value: %d\n",counter);
    
    // context switch
    // thread 2 runs and printf "Counter value: 2"
    printf("Counter value: %d\n",counter);
    

    So, you might end up with this output:

    Counter value: 2
    Counter value: 2
    

    Now, with the mutex in place, you make sure that the increment and its print will run atomically, so you are 100% sure that the output will be:

    Counter value: 1
    Counter value: 2
    

    But never:

    Counter value: 2
    Counter value: 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the source code found on this site here: http://webtint.net/tutorials/5-star-rating-system-in-php-mysql-and-jquery/comment-page-1/#comment-2562 A fantastic resource,
Sorry if this is a comp-sci 101 question. I'm just unsure if I'm missing
Sorry, if this is a noobish question, but I'm just getting started with Rails
I found this script on about.com which I'm trying to learn from on how
I found this script on about.com which I'm trying to learn from on how
Question: Hello All, Sorry that this is kind of a noob question. I just
Sorry guys if this is a lame question, but the examples i found dont
Sorry for this not being a real question, but Sometime back i remember seeing
Sorry, I'm new to SVN and I looked around a little for this. How
Sorry for the second newbie question, I'm a developer not a sysadmin so this

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.