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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:46:56+00:00 2026-06-10T17:46:56+00:00

I am surprised to see from pstack that this code leads to deadlock! I

  • 0

I am surprised to see from pstack that this code leads to deadlock! I don’t see a reason for the same.

pthread_mutex_t lock;

_Cilk_for (int i = 0; i < N; ++i) {
  int ai = A[i];
  if (ai < pivot) {
    pthread_mutex_lock(&lock);
    A[ia++] = ai;
    pthread_mutex_unlock(&lock);
  }
  else if (ai > pivot) {
    pthread_mutex_lock(&lock);
    A[ib++] = ai;
    pthread_mutex_unlock(&lock);
  }
  else {
    pthread_mutex_lock(&lock);
    A[ic++] = ai;
    pthread_mutex_unlock(&lock);
  }
}

I am just using mutexes to make sure that access to A is atomic and serialized.

  • What is wrong with this code to lead to deadlock?
  • Is there a better way to implement this?
  • 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-10T17:46:58+00:00Added an answer on June 10, 2026 at 5:46 pm

    If that’s code inside a function, then you’re not initialising the mutex correctly. You need to set it to PTHREAD_MUTEX_INITIALIZER (for a simple, default mutex) or do a pthread_mutex_init() on it (for more complex requirements). Without proper initialisation, you don’t know what state the mutex starts in – it may well be in a locked state simply because whatever happened to be on the stack at that position looked like a locked mutex.

    That’s why it always needs to be initialised somehow, so that there is no doubt of the initial state.

    Another potential problem you may have is this:

    int ai = A[i];
    

    You probably should protect that access with the same mutex since otherwise you may read it in a “half-state” (when another thread is only part way through updating the variable).


    And, I have to say, I’m not sure that threads are being used wisely here. The use of mutexes is likely to swamp a statement like A[ia++] = ai to the point where the vast majority of time will be spent locking and unlocking the mutex. They’re generally more useful where the code being processed during the lock is a little more substantial.

    You may find a non-threaded variant will blow this one out of the water (but, of course, don’t take my word for it – my primary optimisation mantra is “measure, don’t guess”).

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

Sidebar

Related Questions

Folks I am surprised to see my code working, that I dont have any
I was surprised that the Latex code from a recent question didn't throw up
After upgrading from Firefox 3.6 to FF4 i was surprised to see that jQuery
I am pretty new to silverlight and was very surprised to see that only
Surprised that i havent been able to find this myself, but anyway. Let's say
I was surprised, if I noticed it right, that $this->Js->object() autosorts the array i
I was somewhat surprised to see the below work coming from a C++ background.
I have just installed jre7 and I'm surprised to see that my default locale
consider the following code: perl -wne 'chomp;print if m/[^(?:test)]/' I was surprised to see
Maybe I am not from this planet, but it would seem to me that

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.