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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T16:49:28+00:00 2026-05-14T16:49:28+00:00

I study this code from some book: #include <pthread.h> #include <stdio.h> /* Parameters to

  • 0

I study this code from some book:

#include <pthread.h>
#include <stdio.h>

/* Parameters to print_function.   */
struct char_print_parms {
    /* The character to print. */
    char character;
    /* The number of times to print it.  */
    int count;
};

/* Prints a number of characters to stderr, as given by PARAMETERS,
    which is a pointer to a struct char_print_parms. */
void* char_print(void* parameters) {
    /* Cast the cookie pointer to the right type. */
    struct char_print_parms* p = (struct char_print_parms*) parameters;
    int i;
    for (i = 0; i < p->count; ++i)
        fputc(p->character, stderr);
    return NULL;
}

/* The main program.   */
int main() {
    pthread_t thread1_id;

    pthread_t thread2_id;
    struct char_print_parms thread1_args;
    struct char_print_parms thread2_args;
    /* Create a new thread to print 30,000 ’x’s. */
    thread1_args.character = 'x';
    thread1_args.count = 30000;
    pthread_create(&thread1_id, NULL, &char_print, &thread1_args);
    /* Create a new thread to print 20,000 o’s. */
    thread2_args.character = 'o';
    thread2_args.count = 20000;
    pthread_create(&thread2_id, NULL, &char_print, &thread2_args);
    usleep(20);
    return 0;
}

after running this code, I see different result each time. and some time corrupted result. what is wrong and what the correct way to do that?

  • 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-14T16:49:29+00:00Added an answer on May 14, 2026 at 4:49 pm

    Add:

    pthread_join( thread1_id, NULL);
    pthread_join( thread2_id, NULL); 
    

    to the bottom of your code, before the return in main. Your prosess ends before your threads can complete. A sleep of 20 micro seconds is not enough to let your threads complete executing. Safer to wait for the threads to return.

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

Sidebar

Ask A Question

Stats

  • Questions 502k
  • Answers 502k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You mean like this? <?php session_start(); //home.php if($_SESSION['id']) { ?>… May 16, 2026 at 2:42 pm
  • Editorial Team
    Editorial Team added an answer They are the same thing. If you use the set… May 16, 2026 at 2:42 pm
  • Editorial Team
    Editorial Team added an answer How about: q={!boost b=field(prio)}whatever References: field, BoostQParserPlugin. May 16, 2026 at 2:42 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

This is from my study guide. From my perspective this is almost done but
I'm preparing the SCJP exam with the almost official study book ("SCJP Sun Certified
For reasons I will spare you, I have two weeks to learn some C++.
I am trying to study C, and I am running in to troubles using
When studying a snippet of unknown Python code, I occasionally bump into the varName.methodName()
I've been doing some web development work in PHP recently which has led me
I'm doing a study on large Java projects and would like to view the
Alright, I know this is a simple question, but I can't seem to get
I have a FireWorks-generated HTML page and I added some jQuery to it for
I'm combining these 3 tables into a report query with INNER JOINs (this 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.