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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:09:01+00:00 2026-06-07T08:09:01+00:00

I’m not sure whats wrong with the program below, but it doesn’t print each

  • 0

I’m not sure whats wrong with the program below, but it doesn’t print each and every language once, but randomly some more often, some more less and some wont print

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



char *messages[] = { 
    "English: Hello World!", 
    "French: Bonjour, le monde!",
    "Spanish: Hola al mundo", 
    "Klingon: Nuq neH!",
    "German: Guten Tag, Welt!", 
    "Russian: Zdravstvytye, mir!",
    "Japan: Sekai e konnichiwa!", 
    "Latin: Orbis, te saluto!"
};
#define NUM_MESSAGES (sizeof( messages ) / sizeof( char* ))


void *PrintHello( void *messageid )
{
    pthread_t taskid;
    int *id_ptr, message_num;

    taskid = pthread_self();
    printf( "This is thread with ID %lu.\n", taskid );

    message_num = *((int *) messageid);
    printf( "%s \n", messages[message_num] );

    pthread_exit( NULL );
}

int main( int argc, char *argv[] ) 
{
    pthread_t threads[NUM_MESSAGES];
    int rc, i;


    for( i = 0; i < NUM_MESSAGES; i++ ) {
        void * argument = (void*) &i;
        rc = pthread_create( &threads[i], NULL, PrintHello, argument );
        if( rc ) {
            printf( "ERROR; return code from pthread_create() is %d\n", rc );
            exit( -1 );
        }
    }




    for( i = 0; i < NUM_MESSAGES; i++ ) {
        pthread_join( threads[i], NULL );
    }

    pthread_exit( NULL );
}

I guess the problem is somehow connected with the argument pointer. I tried to lock different parts but with no success.

  • 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-07T08:09:04+00:00Added an answer on June 7, 2026 at 8:09 am

    You’re passing the address of the variable i, which the main thread keeps changing. So you’re at the mercy of scheduling: will anyone change i before your thread has a chance to run ?

    Instead, you could try directly passing the string, greatly simplifying the code:

    rc = pthread_create( &threads[i], NULL, PrintHello, messages[i]);
    
    void *PrintHello(void *arg)
    {
        char *msg = arg;
        printf("%s\n", msg);
    
        return NULL;
    }
    

    There’s another, poorer alternative where you pass the actual value of i as the argument (not its address).

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

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I need a function that will clean a strings' special characters. I do NOT
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string

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.