Created
December 19, 2022 20:37
-
-
Save tiandiao123/37fc577ae19d934e26c617aaa13d9309 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <thread> | |
#include <iostream> | |
using namespace std; | |
class MyThread | |
{ | |
public: | |
void Main(){ | |
cout << "my thread main fuction" << name << " : " << age << endl; | |
} | |
string name; | |
int age = 10; | |
}; | |
class XThread | |
{ | |
public: | |
virtual void Start() | |
{ | |
th_ = std::thread(&XThread::Main, this); | |
} | |
virtual void Wait(){ | |
if(th_.joinable()){ | |
th_.join(); | |
} | |
} | |
private: | |
virtual void Main() = 0; | |
std::thread th_; | |
}; | |
class TestXThread: public XThread | |
{ | |
public: | |
void Main() override{ | |
cout << "Test X Thread Main ------- " << endl; | |
} | |
string name; | |
}; | |
int main(int argc, char* argv[]){ | |
{ | |
cout << "block 1" << endl; | |
MyThread myth; | |
myth.name= "Test Name 001"; | |
myth.age = 20; | |
thread th(&MyThread::Main, &myth); | |
th.join(); | |
} | |
{ | |
TestXThread testth; | |
testth.name = "hello world ... "; | |
testth.Start(); | |
testth.Wait(); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment