したがって、私は一般的にスレッド化にかなり慣れておらず、過去数週間 pthreads を試してきました。内部にスレッド化された関数を持つクラスを作成しました。クラス プロパティ (整数) を値に設定しようとするまでは、正常に動作します。
.h ファイル:
#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 ファイル
#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;
}
では、なぜ私が呼び出すとクラッシュするのmytestint = 42;
ですか?