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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:59:46+00:00 2026-05-28T02:59:46+00:00

I recently endeavoured to learn about multiple threading, and ran into the following unexpected

  • 0

I recently endeavoured to learn about multiple threading, and ran into the following unexpected – to me, at least – behaviour: printf just will not print more than a line at once when called in the very simple following code:

    pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
    pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
    char buffer[2];

    void * thread_routine(void * args){
      pthread_mutex_lock(&mutex);
      pthread_cond_wait(&cond, &mutex);
      printf("test %s\n test\n", buffer);
      pthread_mutex_unlock(&mutex);
      return(NULL);
    }

    int main(void){
      pthread_t thread;
      pthread_create(&thread, NULL, thread_routine, NULL);
      sleep(1);
      buffer[0] = 'c';
      buffer[1] = '\0';
      pthread_mutex_lock(&mutex);
      pthread_cond_signal(&cond);
      pthread_mutex_unlock(&mutex);
      sleep(10);
      return(0);
    }

The output is

    test c

(wait for 10 seconds and)

    test prompt$]

What is wrong with this code? How come I can’t get printf to print two lines at once? Please note that blocking stdout with flockfile and unlocking with funlockfile does nothing to improve the situation.

  • 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-28T02:59:47+00:00Added an answer on May 28, 2026 at 2:59 am

    If your program printed test prompt$] at the end as you say, this means that the version that you executed didn’t have the second newline in "test %s\n test\n".

    Newlines are important, since this is when the output gets flushed to the screen. See Why does printf not flush after the call unless a newline is in the format string? for an explanation and recommendations.

    Try re-compiling and re-running the exact code from your question, and I bet it’ll work as expected (it certainly does on my box).

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

Sidebar

Related Questions

Recently I've been thinking about how to transform a complex polygon into a non-complex
Recently I ran into this error in my web application: java.lang.OutOfMemoryError: PermGen space It's
Recently the PHP manual started showing the following warning on every mysql function page:
Recently I have released my app into the US AppStore. Now I Planed for
Recently I've created one android application using eclipse and ran it using android virtual
Recently just upgraded to SQL Server 2008 R2 Express. When I attempt to create
recently I started using storyboard and I've the following situation: I want to set
Recently users of my website complained about the lack of support to IE6\7. Is
Recently a friend and I were talking about securing stored procedure code in a
recently I am reading about Thinking in Java. that code in my PC is

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.