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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:27:14+00:00 2026-05-12T20:27:14+00:00

The following code produces wrong and inconsistent output with gcc (4.1.2 20080704) but correct

  • 0

The following code produces wrong and inconsistent output with gcc (4.1.2 20080704) but correct and expected output with icc (Version 11.1) . But when I moved the thread_data_array[] definition from main() to global (immediately after the struct thread_data definition) it works fine with both compilers. I do not see why this change should make any difference. I would like to generate threads recursively so I need to call it from a function but not define as global. Could someone explain what is wrong the code please?

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

#define NUM_THREADS 4

struct thread_data {
    int  thread_id;
    int  sum;
};

/* struct thread_data thread_data_array[NUM_THREADS]; */

void *p_task(void *threadarg)
{
    struct thread_data *my_data;
    int taskid;
    int sum;

    my_data = (struct thread_data *) threadarg;
    taskid = my_data->thread_id;
    sum = my_data->sum;

    printf("Thread #%d with sum %d\n", taskid, sum);

    for ( sum = 0; sum < 000000000; sum++ ) {
        for ( taskid = 0; taskid < 000000000; taskid++ ) {
            sum+=taskid;
        }
    }

    return my_data;
}

int main ()
{
    struct thread_data thread_data_array[NUM_THREADS]; /*this does not work*/

    pthread_t threads[NUM_THREADS];
    int rc;
    long t;

    for ( t = 0; t < NUM_THREADS; t++ ) {
        thread_data_array[t].thread_id = t;
        thread_data_array[t].sum = (int) t*2;
        rc = pthread_create( &threads[t], NULL, p_task, (void *) &thread_data_array[t] );
    }

    pthread_exit(NULL);

    return 0;

}
  • 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-12T20:27:14+00:00Added an answer on May 12, 2026 at 8:27 pm

    Well, my first thought would be that the main thread is actually exiting, and taking its stack frame with it, before the other threads are finished. From what I remember about pthreads (and this is over a decade and a half ago, using them with DCE), there’s absolutely nothing special about the main thread other than the stack size.

    If the array is declared global, the exit from main will have no effect on it but, if it’s in the stack frame for main, I’d be very careful using it after it exits.

    I’m basing this partially on guesswork since you haven’t actually explained what the behavior is that you’re seeing.

    I would suggest inserting the following code before the pthread_exit in main:

    for (t = 0; t < NUM_THREADS; t++ )
        pthread_join (threads[t], NULL);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The list of keyboard shortcuts can be found in the… May 13, 2026 at 9:56 am
  • Editorial Team
    Editorial Team added an answer Colorbox is nice. May 13, 2026 at 9:56 am
  • Editorial Team
    Editorial Team added an answer I think (like Ed said) I would resolve this by… May 13, 2026 at 9:56 am

Related Questions

The following code produces random values for both n and v. It's not surprising
The following code produces an error hr=0x80020005 (wrong type). #import <msi.dll> using namespace WindowsInstaller;
I'm playing around with .NET's configuration support (the ConfigurationManager class and related support classes).
I learned in THIS THREAD how a hashed_unique<> index can be used with a

Trending Tags

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

Top Members

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.