c++0x 学习笔记之 多线程(1) ― 启动线程

2014-11-24 13:01:27 · 作者: · 浏览: 0

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">BAIDU_CLB_fillSlot("771048");
点击复制链接 与好友分享! 回本站首页
<script> function copyToClipBoard(){ var clipBoardContent=document.title + '\r\n' + document.location; clipBoardContent+='\r\n'; window.clipboardData.setData("Text",clipBoardContent); alert("恭喜您!复制成功"); }
分享到: 更多
<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 学习笔记之 Function template bind
下一篇: 《21天学通C++》第一周复习清单
相关文章
C++0x 学习笔记之 Variadic Templates
<script type="text/java script">BAIDU_CLB_fillSlot("182716");
<script type="text/java script">BAIDU_CLB_fillSlot("517916");
图文推荐
<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("771057");
排行
热门
<script type="text/java script">BAIDU_CLB_fillSlot("406189");
<script type="text/java script">BAIDU_CLB_fillSlot("703749");
<iframe frameborder="0" name="Iframe1" src="http://www.2cto.com/bbsdy/index.html" width="100%" height="200"> 您的浏览器不支持嵌入式框架,或者当前配置为不显示嵌入式框架。
<script type="text/java script">BAIDU_CLB_fillSlot("182692");
文章
下载
读书
<script type="text/java script">BAIDU_CLB_fillSlot("771043");
<script type="text/java script"> <script language="java script" src="http://www.2cto.com/api.php op=count&id=89773&modelid=1"> <script type="text/java script">BAIDU_CLB_fillSlot("137946");
<script type="text/java script">BAIDU_CLB_fillSlot("333829");

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训
版权所有: 红黑联盟--致力于做最好的IT技术学习网站<script type="text/java script"> var _bdhmProtocol = (("https:" == document.location.protocol) " https://" : " http://"); document.write(unescape("%3Cscript src='" + _bdhmProtocol + "hm.baidu.com/h.js%3F1898984a3d796e86ad73ad1f4bc9f240' type='text/java script'%3E%3C/script%3E"));