C++11新しい標準スレッドライブラリの方法5
#include
#include
#include
#include
using namespace std;
class A
{
public:
//
A(int i) :m_i(i)
{
cout << " "<< endl;
}
A(const A & a) :m_i(a.m_i)
{
cout << " " << endl;
}
~A()
{
cout << " " << endl;
}
private:
int m_i;
};
void myprint(const int i ,const A& mybuf)
{
//
cout << &mybuf << endl;
}
int main()
{
int i = 7;
int j = 9;
//j A
thread myobj(myprint,i,j);
// myprint()
myobj.join();// , , , ,
cout << "I love china !" << endl;
return 0;
}
#include
#include
#include
#include
using namespace std;
class A
{
public:
//
A(int i) :m_i(i)
{
cout << " "<< endl;
}
A(const A & a) :m_i(a.m_i)
{
cout << " " << endl;
}
~A()
{
cout << " " << endl;
}
private:
int m_i;
};
void myprint(const int i ,const A& mybuf)
{
//
cout << &mybuf << endl;
}
int main()
{
int i = 7;
int j = 9;
//j A
thread myobj(myprint,i,A(j));
myobj.detach();// , , , , int i ,j A a
myprint()
//if (myobj.joinable)
//{
// myobj.join();// , , , ,
//}
cout << "I love china !" << endl;
return 0;
}
//myobjを使う.detach()注意総括://(a)intという簡単なタイプのパラメータを渡す場合は、値伝達であることをお勧めします.参照を使わないで、節外生枝//(b)クラスオブジェクトを渡す場合は、暗黙的なタイプ変換を避けることを防止します.全治療はスレッドを作成する行で一時的なオブジェクトを構築し、関数パラメータで参照で接続します.そうしないと、一度に構築し、二次コピー//まとめます.detch()を使用せずにjoin()のみを使用することをお勧めします.これにより、ローカル変数が無効になり、スレッドによるメモリへの不正な参照の問題が発生しません.
// ID
//c++ std::this_thread::get_id()
#include
#include
#include
#include
using namespace std;
class A
{
public:
//
A(int i) :m_i(i)
{
cout << " "<< this << "threadid =" << std::this_thread::get_id() << endl;
}
A(const A & a) :m_i(a.m_i)
{
cout << " " << this << "threadid =" << std::this_thread::get_id() << endl;
}
~A()
{
cout << " " << this << "threadid =" << std::this_thread::get_id() << endl;
}
private:
int m_i;
};
void myprint(const A& mybuf)
{
//
cout << "myprint" << &mybuf << "threadid =" << std::this_thread::get_id() << endl;
}
int main()
{
// ID
cout << " iD:" << "threadid =" << std::this_thread::get_id() << endl;
int j = 9;
//j A ,A , A
thread myobj(myprint,j);// A ( )
thread myobj(myprint, A(j));//A , A ,( )
// myprint()
if (myobj.joinable())
{
myobj.join();// , , , ,
}
cout << "I love china !" << endl;
return 0;
}