引用内部函数绑定机制,R转义字符,C++引用,别名,模板元,宏,断言,C++多线程,C++智能指针(二)

2015-07-20 17:53:44 · 作者: · 浏览: 15

#include

#include

#include

#include

usingnamespacestd;

usingnamespacestd::this_thread;

voidmsg()

{

MessageBoxA(0,"12345","678910",0);

}

voidmsgA(intnum)

{

std::cout << get_id() <<" num = " <

}

voidmain()

{

vector threads;

for (inti = 0;i < 10;i++)

{

threads.push_back(newthread(msg));//创建线程

}

for (autoth :threads)

{

th->join();

}

std::cin.get();

}

\

10.线程间通信

#include

#include

#include

#include

usingnamespacestd;

usingnamespacestd::this_thread;

voidmsgA(intnum)

{

std::cout << get_id() <<" num = " <

}

voidmain()

{

vector threads;

for (inti = 0;i < 10;i++)

{

//其中后面的msgA为函数名,i为为函数传递的参数

threads.push_back(newthread(msgA,i));//创建线程

}

for (autoth :threads)

{

th->join();

}

std::cin.get();

}

程序运行结果如下:

\

11.C++中的智能指针

#include

#include //内存

voidmain()

{

for (inti = 0;i < 10000000;i++)

{

//新型指针,新型的数组

std::unique_ptr pdb(newdouble);

//通过指针执行来自动释放内存

//double *p = new double;

}

std::cin.get();

}

12.另外一种智能指针

#include

voidmain()

{

//auto_prt

for (inti = 0;i < 10000000;i++)

{

double *p = newdouble;//为指针分配内存

std::auto_ptr autop(p);

//创建智能指针管理指针p指向内存

//智能指针

//delete p;

}

std::cin.get();

}