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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:20:51+00:00 2026-05-11T11:20:51+00:00

I am working on a project and trying to use pthread_cond_wait() and pthread_cond_signal() to

  • 0

I am working on a project and trying to use pthread_cond_wait() and pthread_cond_signal() to synchronize two threads.

My code looks something like this:

pthread_mutex_t lock_it = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t write_it = PTHREAD_COND_INITIALIZER;     int main(int argc, char**argv)   {       pthread_t t_send_segments, t_recv_acks;       pthread_create(&t_send_segments, NULL, send_segments, (void*)NULL);      pthread_create(&t_recv_acks,     NULL, recv_acks,     (void*)NULL);       pthread_join(t_recv_acks, (void**)NULL);       pthread_mutex_destroy(&lock_it);      pthread_cond_destroy(&write_it); }   void* send_segments(void *v) {     for(;;) {        pthread_mutex_lock(&lock_it);        printf('s1\n');        printf('s2\n');        pthread_cond_wait(&write_it, &lock_it);        printf('s3\n');        printf('s4\n');        printf('s5\n');         pthread_mutex_unlock(&lock_it);     }     return 0;  }   void* recv_acks(void *v) {     for(;;) {        pthread_mutex_lock(&lock_it);        printf('r1\n');        pthread_cond_signal(&write_it);        printf('r2\n');        pthread_mutex_unlock(&lock_it);     }       return 0;  } 

The expected output is:

s1 s2 r1 s3 s4 s5 s1 s2 r2 r1 s3 s4 s5  (etc) 

My output doesn’t follow this pattern at all. Clearly I have a logic error somewhere, but I’m not understanding where. Why doesn’t the recv_acks() thread always yield when it hits the pthread_cond_signal() – since the pthread_cond_wait() always executes first (because of the order in which I create the threads) and the cond_wait() always executes since its in the critical section?

  • 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. 2026-05-11T11:20:52+00:00Added an answer on May 11, 2026 at 11:20 am

    The pthread_cond_signal function does not cause the current thread to yield and does not release the mutex. All it does is restart one thread that has suspended itself on the condition via pthread_cond_wait. This just means the awakened thread is available for scheduling, it doesn’t cause it to execute immediately. The thread-scheduler will schedule it sometime in the future.

    Also, just because the s-thread has been awakened and is contending for the mutex, that doesn’t mean it’s going to get the mutex next. Mutexes are not necessarily fair to all threads that have requested it. According to the pthread_mutex man page: ‘pthread_mutex_lock locks the given mutex. If the mutex is currently unlocked, it becomes locked and owned by the calling thread, and pthread_mutex_lock returns immediately.’ So the r-thread can spin in its loop several times, happily unlocking and relocking the mutex several times before being swapped out by the scheduler. This means the s-thread will only get a chance at the mutex if the scheduler happens to interrupt the r-thread during the brief time in which it has released the mutex.

    To achieve the output you want, both threads will need to control their execution with a condition and signal each other before suspending themselves. However, this may or may not be what you actually want to do with your real project.

    Another note: it doesn’t really matter what order you created the threads in. Creating a thread does not yield the creating thread. So the main thread will probably create both threads before either gets scheduled, and the thread scheduler is free to schedule either one of them for execution next. If the s-thread does run first on your platform, that just happens to be the implementation behavior on your platform and is not something that should be relied on.

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

Sidebar

Related Questions

I am trying to use threads in a Python project I am working on,
I'm working on a small project (Grails) and I am trying to use this
Trying to use Linq to SQL for a small project I'm working on at
I'm trying to use form validation that is working in this example with my
I'm trying to use the pseudo class ( img:nth-child) in this project but I
I'm trying to use application settings with a C#.NET project I am working on.
I'm trying to use javascript as little as possible on this project. I would
I'm working on a project in C#.Net 4.0. I am trying to use HttpUtility.HtmlDecode.
I am working on an Actionscript 2 project - trying to use the XML
I'm trying to use clang to profile a project I'm working on. The project

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.