C++11 thread编程调用类方法并传入参数

2 篇文章 0 订阅

#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---

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值