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

  • Home
  • SEARCH
  • 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 7720559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:42:22+00:00 2026-06-01T03:42:22+00:00

Well I’m trying to set a variable to use in a thread, it works

  • 0

Well I’m trying to set a variable to use in a thread, it works fine if I call the pthread from the main function, but if I call it from a function or a function inside a class, the variable is lost and prints garbage instead, that’s why I added the condition

if(this->pickup < 7)

so I minimized the code so I could post it here because it has all the examples I’m saying.

The output of this code below is:

Access by Class:

Hello, world! <

Access Directly:

Hello, world!, N: 6<

I would like to have the same result as in Access Directly in Access by Class, I want it to output the “, N: 6” because after all it was defined. what am I missing here?

I hope I was clear enough, thanks in advance.

(By the way, I’m using the pthread library that is available for windows)
so here is the code:

#include <stdio.h>
#include <pthread.h>
#include <iostream>
#include <conio.h>
#include <windows.h>

class C {
public:
    int pickup;

    void *hello()
    {
        std::cout << "\nHello, world!";

        if(this->pickup < 7)
        std::cout << ", N: " << this->pickup;

        std::cout << "<" << std::endl;
        printf("HI");
        return 0;
    }

    static void *hello_helper(void *context)
    {
        return ((C *)context)->hello();
    }


    void StartThread(){
        C c;
        c.pickup = 6;

        pthread_t t;
        pthread_create(&t, NULL, &C::hello_helper, &c);
    }


};



int main () {

    C c;
    std::cout << "Access by Class: \n";
    c.StartThread();
    c.pickup = 6;
    Sleep(2000);

    std::cout << "\nAccess Directly: \n";
    pthread_t t;
    pthread_create(&t, NULL, &C::hello_helper, &c);

    _getch();
    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-06-01T03:42:23+00:00Added an answer on June 1, 2026 at 3:42 am

    c is destroyed when StartThread() returns, meaning hello_helper() is using a dangling pointer resulting in undefined behaviour.

    Change to:

    void StartThread(){
        C* c = new C();
        c->pickup = 6;
    
        pthread_t t;
        pthread_create(&t, NULL, &C::hello_helper, c);
    }
    

    Remember to delete the argument passed into hello_helper():

    static void *hello_helper(void *context)
    {
        C* c = static_cast<C*>(context);
        c->hello();
        delete c;
        return 0;
    } 
    

    EDIT:

    Always deleteing the argument passed into hello_helper() would prevent passing stack allocated objects into hello_helper(). A mechanism is required to instruct hello_helper() whether it is responsible for destructing its argument.

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

Sidebar

Related Questions

Well... simple question, right? But with no so simple answers. In firefox i use
Well friends, I have got this query which works but is very long for
Well, I'm trying to make a graphical game for the first time, but: BufferedImage
Well, I know how to handle associations from one model (call it ModelA) through
well this is my problem i use 2 source first query (select * from
Well, it seems simple enough, but I can't find a way to add a
Well I created a jquery mobile form but the every element is in a
Well, I know there is a lots of posts about it, but I have
well. i was use Reactive Extensions (Rx) package async Request,Before you can start writing
well I'm trying to create a great playlist of music, only using a sigle

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.