#include <thread>
#include <iostream>
class classA
{
public :
classA()
{
std::cout<<" classA " <<std::endl;
}
void func(int a, const char *pchText)
{
std::cout<<" func " <<std::endl;
m_a = a;
std::cout<<" func " << m_a << " " << pchText <<std::endl;
}
void func2()
{
std::cout<<" func2 " <<std::endl;
m_a = 4;
std::cout<<" func2 " << m_a << "---kkk---" <<std::endl;
}
void do_func()
{
//std::thread t(&classA::func,this , 444,"eeee");
std::thread t(&classA::func, this, 444 , "func");
t.detach();
//t.join();
}
void do_func2()
{
//std::thread t(&classA::func,this , 444,"eeee");
//std::thread t(&classA::func, this,444,"eeee");
std::thread t(&classA::func2, this );
t.detach();
//t.join();
}
private :
int m_a;
};
int main()
{
classA A;
A.do_func();
A.do_func2();
while(1)
{}
}
编译 g++ -std=c++11 -pthread thread_test.cpp
root@ubuntu-Lenovo:/share# g++ -std=c++11 -pthread thread_test.cpp
root@ubuntu-Lenovo:/share# ./a.out
classA
func
func 444 func
func2
func2 4---kkk---