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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:35:20+00:00 2026-06-04T21:35:20+00:00

I am learning multithreading performance in C. When I tried to write a sample

  • 0

I am learning multithreading performance in C. When I tried to write a sample code, I bumped into a problem:

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

typedef struct{
    int a;
    char b;
    } args;

void* some_func (void* arg)
{
    args *argsa = malloc(sizeof(args));
//copy the content of arg to argsa, 
//so changes to arg in main would not affect argsa
    *argsa = *(args*) arg; 
    int i = 10;
    for (; i > 0; i--)
    {
        usleep (1); //to give other threads chances to cut in
        printf ("This is from the thread %d\n", argsa->a);
    }
    free (argsa);
}
int main()

{
    pthread_t thread[3];
    args ss;
    int index = 0;
    ss.b = 's';
    for (; index <3 ; index++)
    {
        ss.a = index;
        if (pthread_create (thread+index, NULL, some_func, (void*)&ss ))
        {
            usleep(10);
            printf ("something is wrong creating the thread"); 
        }
    }
        pthread_join ( thread[0], NULL);
        pthread_join ( thread[1], NULL);
        pthread_join ( thread[2], NULL);
    return 0;
}

I know char b in the struct is useless, but I just want to practice passing a structure.
I expect the code to print out “This is from the thread x”, where x is 0, 1 or 2, alternatively. However, the code currently only gives me “This is from the thread 2” 30 times. I believe there is something wrong with

*argsa = *(args*) arg; 

But I can’t find a way to solve this and get the desired output.

Any help would be appreciated!

  • 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-04T21:35:21+00:00Added an answer on June 4, 2026 at 9:35 pm

    Because you are passing the same pointer to all the threads. By the time thread 0 has started, you have already incremented the value of ss.a to 1 (and then 2).

    This is a bit more correct:

    void* some_func (void* arg)
    {
        args *argsa = (args*)arg;
        int i;
        for (i = 0; i < 10; i++)
        {
            usleep (1); //to give other threads chances to cut in
            printf ("This is from the thread %d\n", argsa->a);
        }
    }
    int main()
    {
        pthread_t thread[3];
        args ss[3];
        int index;
        for (index = 0; index < 3; index++)
        {
            ss[index].a = index;
    
            if (pthread_create(&thread[index], NULL, some_func, &ss[index] ))
            {
                printf ("something is wrong creating the thread"); 
            }
        }
        pthread_join ( thread[0], NULL);
        pthread_join ( thread[1], NULL);
        pthread_join ( thread[2], NULL);
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am learning Perl's multithreading. My code: use warnings; use threads; use threads::shared; $howmany
Learning the hard way, I tried to left shift a long long and uint64_t
Learning xml, Can anyone help me? I have following XML code: **<book lang=en>name of
I'd like to start learning multithreading in C++. I'm learning it in Java as
I am learning about threading and multithreading..so i just created a small application in
Learning spine.js I completed both the tutorials no problem, seems like a great framework,
I am just learning how to write mutithreaded programs right now and I have
Learning to use Ruby Threads for transportability of code between different OS platforms. The
learning about loops (still a beginner) in VB.net. I have got the below code
I'd like to write a Map-Extension Method for ParallelQuery without destroying the parallelism. Problem

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.