by feng
编译环境
编译器: g++ 4.5
编译选项: -std=c++0x
链接选项: pthread
完整编译链接命令: g++ O2 o example example.cc -std=c++0x -pthread
头文件:
| 条目 | 头文件 |
| thread | |
| Mutual exclusion | |
| Condition variables | |
| Futures |
a 线程创建
I 从函数中创建
如果想在一个线程中执行一个函数 f ,那么这个线程可以这样创建:
|
1
|
std::
thread
t( f );
|
如果函数有参数,那么直接把参数列在后边即可:
|
1
2
3
4
5
|
void
hello_from(
const
char
* str
const
)
{
std::cout <<
"hello from "
<< str <<
" "
;
}
std::
thread
t( hello_from,
"thread t"
);
|
多个参数的函数也是如此:
|
1
2
3
4
5
|
void
max(
const
long
m,
const
long
n )
{
std::cout <<
"max("
<< m <<
", "
<< n <<
")="
<< (m>n m:n) <<
" "
;
}
std::
thread
t( max, 13, 31 );
|
可以依此类推到3个、4个……参数的函数情形。
只要不把 main 函数也弄进去,编译器统统接受:
|
1
2
3
4
|
void
try_start_program_here()
{
std::
thread
t( main );
//error
}
|
II 从对象/仿函数中创建
把仿函数依样搬进去:
|
1
2
3
4
5
6
7
|
struct
say_hello
<script type="text/java script" id="bdshare_js" data="type=tools&uid=12732">
<script type="text/java script" id="bdshell_js">
<script type="text/java script">
var bds_config = {'snsKey':{'tsina':'2386826374','tqq':'5e544a8fdea646c5a5f3967871346eb8'}};
document.getElementById("bdshell_js").src = "http://bdimg.share.baidu.com/static/js/shell_v2.js cdnversion=" + Math.ceil(new Date()/3600000)
C++0x 学习笔记之 Variadic Templates
<iframe src="http://www.2cto.com/uapi.php tid=89773&catid=339&title=YysrMHgg0afPsLHKvMfWriC24M/fs8woMSkgoaogxvS2r8/fs8w=&forward=http://www.2cto.com/kf/201105/89773.html" width="100%" height="100%" id="comment_iframe" name="comment_iframe" frameborder="0" scrolling="no">
<script type="text/java script">BAIDU_CLB_fillSlot("406189");
<script type="text/java script">BAIDU_CLB_fillSlot("703749");
<script type="text/java script">BAIDU_CLB_fillSlot("182692");
<script type="text/java script">
<script type="text/java script">BAIDU_CLB_fillSlot("771043");
<script type="text/java script">BAIDU_CLB_fillSlot("333829");
关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 |






