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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:53:33+00:00 2026-06-15T13:53:33+00:00

I have a general question that may be easy to answer, but it might

  • 0

I have a general question that may be easy to answer, but it might not be. I’m not sure. In my code I have four threads running, all working to work their way through a list, each popping a piece of it each time they run. Below is the code for each thread, where they wait for the mutex to unlock, lock it themselves, do their thing, and then relock the mutex.

My question is this: Is there a way to pass the lock to a specific thread?\

For example: If I had four threads running this code (t1, t2, t3, and t4) and t1 just finished, is there a way that I can guarantee that t1 will pass the lock to t2, and t2 will pass the lock to t3, etc?

void *consumer(void *ptr){
    int i;

    // Print the thread ID
    printf("Consumer TID %lu\n", (unsigned long)gettid());

    // While the list isn't empty
    while (1){
    // Wait for the mutex
    pthread_mutex_lock(&mutex);
        // When you get it...

        // If the list is empty
        if (the_list.empty()){
            // Unlock the mutex
            pthread_mutex_unlock(&mutex);
            break;
        }

        // Otherwise remove an element from the list
        the_list.pop_front();

        // And unlock the mutex
        pthread_mutex_unlock(&mutex);
    }

    return NULL;
}

Thank you!

UPDATE – RANDOM THOUGHT: I just had a thought. Could there be four different functions that each took in a specific mutex only released by the previous function?

  • 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-15T13:53:35+00:00Added an answer on June 15, 2026 at 1:53 pm

    Your problem with the consumer is that it needs to stay ready even if the list gets empty.

    I say this because it might be the case that the consumer is up-and-ready before the first producer comes. In that situation the consumer sees an empty list and exits, thus no produced elements will be consumed.

    So I would suggest rather

    pthread_mutex_lock(&mutex);
    while(the_list.empty())
      pthread_cond_wait(&produced, &mutex);
    pthread_mutex_unlock(&mutex);
    

    And the producer(s) will signal you the signal pthread_cond_signal(&produced).

    I think this also yields an answer to your question: you don’t “pass the lock”, but signal (and wait for the signal — if you are interested in the “passing of the lock” you could do it with 3-4 different signals).


    As for the locks: the locks are per-data not per-thread. So the lock is usually stored together with the data.

    struct threadsafe_data_t {
      data_t data;
      pthread_mutex_t mutex;
      pthread_cond_t modified; // or produced or whatever 
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a bit of a javascript noob, but I have a general question that
I have a Yesod application (but question is more general than this) that allows
I have a specific question, that could use a general answer... When building Multi-Tier
I have a general question, that may be a little compiler-specific. I'm interested in
I have a general question that is rather open-ended (i.e. depends on platform, application
I have a general question about the way that database indexing works, particularly in
I have a general question on the class definition and its use..THe below code
I'm looking for an answer that scales, but for my specific purpose, I have
Just a very general question, that not only applies to this example. Let's say
This is more of a design question that others may have had similar experience

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.