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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:51:58+00:00 2026-06-18T05:51:58+00:00

So I am pretty new to threading in general and have been experimenting with

  • 0

So I am pretty new to threading in general and have been experimenting with pthreads for the past couple of weeks. I have created a class that has a threaded function within itself. It works fine, until I tried to set a class property (an integer) to a value.

.h file:

#ifndef TESTCLASS_H
#define TESTCLASS_H

#include <iostream>
#include <windows.h>

using namespace std;

class testClass
{
    public:
        testClass();
        HANDLE h;
        static DWORD WINAPI mythread(LPVOID param);
        int mytestint;
        void printstuffs();
        void startThread();
};

#endif // TESTCLASS_H

.cpp file

#include "testClass.h"

testClass::testClass()
{
    cout << "Created class" << endl;
}
DWORD WINAPI testClass::mythread(LPVOID param)
{
    cout << "In thread" << endl;
    testClass* This = (testClass*)param;

    cout << "Calling class function" << endl;
    This->printstuffs();

    cout << "Thread is done" << endl;

    return NULL;
}
void testClass::printstuffs()
{
    cout << "In class function " << endl;
    mytestint = 42; // <- crashes here
    cout << "Test Int = " << mytestint << endl;
}
void testClass::startThread()
{
    h = CreateThread(NULL, 0, mythread, (LPVOID)0, 0, NULL);
    cout << "Thread started" << endl;
}

So why does it crash when I call mytestint = 42; ?

  • 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-18T05:52:00+00:00Added an answer on June 18, 2026 at 5:52 am

    You’re calling mythread with a null pointer. When you cast that to This, you end up calling a function on a null object. When you do mytestint = 42, the computer sees it like this->mytestint = 42, and since this is NULL, you dereference a null pointer, and the program segfaults. You need to do something like the following:

    h = CreateThread(NULL, 0, mythread, (LPVOID)this, 0, NULL);
    

    If possible, I would also suggest migrating over to standard C++ threads introduced in C++11. Since it looks like you’re just learning multithreading it would be useful to learn the standard facilities (which are included with the latest versions of MSVC and GCC) vice vendor-specific APIs.

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

Sidebar

Related Questions

Pretty new to threading and I have this QList that the threads share between
Pretty new to MVC and the like. I have a class the looks like
I am pretty new to multi-threading in general and I would like to know
I'm still pretty new to ObjC. I noticed that it's pretty standard everywhere to
I am pretty new to multi threading and tried something to apply it, but
Possible Duplicate: C# Captured Variable In Loop I am pretty new to multi-threading programming.
The title pretty much explains the issue. I have a button that when I
Alright, I'm new to threading, so my question might be pretty dumb. But what
I've finished writing a new application which has now been deployed into the live
Pretty new (2 weeks) into Objective-C and Xcode, and I'm trying to add my

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.