ImageVerifierCode 换一换
格式:DOCX , 页数:24 ,大小:24.43KB ,
资源ID:2880992      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/2880992.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(C++11多线程例子.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

C++11多线程例子.docx

1、C+11多线程例子ThreadsThestd:threadclass represents a thread of execution and is available in theheader.std:threadcan work with regular functions, lambdas and functors (a class implementing operator(). Moreover it allows you to pass any number of parameters to the thread function.Collapse|Copy Code#includ

2、e void func() / do some work int main() std:thread t(func); t.join(); return 0;In this exampletis a thread object representing the thread under which functionfunc()runs. The call tojoinblocks the calling thread (in this case the main thread) until the joined thread finishes execution. If the thread

3、function returns a value, it is ignored. However, the function can take any number of parameters.Collapse|Copy Codevoid func(int i, double d, const std:string& s) std:cout i , d , s std:endl; int main() std:thread t(func, 1, 12.50, sample); t.join(); return 0;Even though its possible to pass any num

4、ber of parameters to the thread function, all parameters are passed by value. If the function needs to take parameters by reference, the passed arguments must be wrapped in astd:ref or std:creflike in the following example.Collapse|Copy Codevoid func(int& a) a+; int main() int a = 42; std:thread t(f

5、unc, std:ref(a); t.join(); std:cout a std:endl; return 0;The program prints 43, but without wrapping a in astd:refthe output would be 42.Apart from the join method, the thread class provides a couple more operations: swap: exchanges the underlying handles of two thread objects detach: allows a threa

6、d of execution to continue independently of the thread object. Detached threads are no longer joinable (you cannot wait for them).Collapse|Copy Codeint main() std:thread t(funct); t.detach(); return 0;An important thing to note is that if a thread function throws an exception it wont be caught with

7、a regular try-catch block. In other words, this wont work:Collapse|Copy Codetry std:thread t1(func); std:thread t2(func); t1.join(); t2.join();catch(const std:exception& ex) std:cout ex.what() std:endl;To propagate exceptions between threads you could catch them in the thread function and store them

8、 in a place where it can be accessed later.Collapse|Copy Codestd:mutex g_mutex;std:vector g_exceptions;void throw_function() throw std:exception(something wrong happened);void func() try throw_function(); catch(.) std:lock_guard lock(g_mutex); g_exceptions.push_back(std:current_exception(); int main

9、() g_exceptions.clear(); std:thread t(func); t.join(); for(auto& e : g_exceptions) try if(e != nullptr) std:rethrow_exception(e); catch(const std:exception& e) std:cout e.what() std:endl; return 0;For more information about catching and propagating exceptions you can readHandling C+ exceptions throw

10、n from worker thread in the main threadandHow can I propagate exceptions between threads?.Before going further it worth noting that theheader provides some helper functions in namespacestd:this_thread: get_id: returns the id of the current thread yield: tells the scheduler to run other threads and c

11、an be used when you are in a busy waiting state sleep_for: blocks the execution of the current thread for at least the specified period sleep_util: blocks the execution of the current thread until the specified moment of time has been reachedLocksIn the last example I needed to synchronize the acces

12、s to theg_exceptionsvector to make sure only one thread at a time can push a new element. For this I used a mutex and a lock on the mutex. A mutex is a core synchronization primitive and it C+11 it comes in four flavors in theheader. mutex: provides the core functionslock()andunlock()and the non-blo

13、ckingtry_lock()method that returns if the mutex is not available. recursive_mutex: allows multiple acquisitions of the mutex from the same thread. timed_mutex: similar to mutex, but it comes with two more methodstry_lock_for()andtry_lock_until()that try to acquire the mutex for a period of time or u

14、ntil a moment in time is reached. recursive_timed_mutex: is a combination of timed_mutex and recusive_mutex.Here is an example for using astd:mutex(notice the use of theget_id()andsleep_for()helper functions mentioned earlier).Collapse|Copy Code#include #include #include #include std:mutex g_lock; void func() g_lock.lock(); std:cout entered thread std:this_thread:get_id() std:endl; std:this_thread:sleep_for(std:chrono:seconds(rand() % 10); std:cout l

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1