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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:02:20+00:00 2026-06-12T02:02:20+00:00

I am using the above code to increment a counter using 2 threads, which

  • 0

I am using the above code to increment a counter using 2 threads, which independently take the mut lock and increment counter. I am facing a deadlock after the threads enter this function.

 pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;

 void *increment_counter(void *counter_addr)
{
    int max = MAX_COUNTER_VALUE;
    int iter;
    int counter;

    for(iter=0;iter< max ;iter++)
   // LOCK  
    pthread_mutex_lock(&mut);
    counter++;
    // UNLOCK 
    pthread_mutex_unlock(&mut);
    return NULL; 
}

Could anyone please tell me where exactly am I going wrong?

  • 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-12T02:02:22+00:00Added an answer on June 12, 2026 at 2:02 am

    You’re trying to lock the mutex max times, then increment counter and release it once.

    Try:

    for(iter=0;iter< max ;iter++)
    {
      // LOCK  
      pthread_mutex_lock(&mut);
      counter++;
      // UNLOCK 
      pthread_mutex_unlock(&mut);
    }
    return NULL; 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Directory.GetFiles(targetDirectory); Using the above code we get the names( i.e full path) of all
<input type=number name=amount max=6 /> HTML5 and PHP I am using the above code
copy('https://graph.facebook.com/$fbid/picture?type=large', 'images/$fbid.jpg'); i am using the above code to store the image locally ..
I am using something like the above sample code but when i try to
When I execute the code saveXML below it generates the error above, why?? using
Hi i want to make a request for soap using above soap xml <soapenv:Envelope
I'm using NSURLConnection with sendAsynchronousRequest method (and handing the data in block). Using above
Ref: Loading gravatar using jquery I am using the above method to generate the
/^[a-d][a-d]*(?:_[a-d]+)*$/ I'm using the above regex in jquery where I call it on every
I have the following: $(span.findme).each(function() { $(<li>).text($(this).text()).appendTo(ul); }); How can I, using the above,

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.