当前位置: 首页 > news >正文

浦东新区网站建设推广深圳的网站建设公司哪家好

浦东新区网站建设推广,深圳的网站建设公司哪家好,织梦cms手机网站,wordpress信用卡std::recursive_timed_mutex允许同一线程多次获取锁#xff0c;并提供了超时功能。 这种锁特别适合用在递归函数中#xff0c;或者当一个操作可能在同一线程内多次尝试获取同一锁时使用。 与std::timed_mutex一样#xff0c;std::recursive_timed_mutex也提供了try_lock_f…std::recursive_timed_mutex允许同一线程多次获取锁并提供了超时功能。 这种锁特别适合用在递归函数中或者当一个操作可能在同一线程内多次尝试获取同一锁时使用。 与std::timed_mutex一样std::recursive_timed_mutex也提供了try_lock_for()和try_lock_until()方法。 下面通过两个使用std::recursive_timed_mutex的示例展示它在递归函数中的用法以及如何利用超时机制。 try_lock_for()用法示例 假设我们有一个递归函数recursive_access该函数在每一层递归中都尝试获取同一个锁。 我们使用std::recursive_timed_mutex来避免死锁并利用超时机制在不能获取锁时做出适当的响应。 #include iostream #include mutex #include thread #include chronostd::recursive_timed_mutex rtmx;void recursive_access(int level, int maxLevel) {// 使用try_lock_for尝试带超时的锁定if (rtmx.try_lock_for(std::chrono::milliseconds(100))) {if (level maxLevel) {std::cout Level level - Lock acquired by thread std::this_thread::get_id() std::endl;// 递归调用进入下一层recursive_access(level 1, maxLevel);rtmx.unlock();} else {std::cout Max recursion level maxLevel reached, unlocking... std::endl;}rtmx.unlock();} else {// 超时处理逻辑std::cout Thread std::this_thread::get_id() could not acquire the lock at level level std::endl;} }int main() {std::thread t1(recursive_access, 0, 3);std::thread t2(recursive_access, 0, 3);t1.join();t2.join();return 0; }在这个例子中我们定义了一个递归函数recursive_access它尝试获取一个std::recursive_timed_mutex。 通过try_lock_for我们设置了一个100毫秒的等待时间如果在这段时间内成功获取了锁则递归调用继续执行如果等待超时则输出一条消息并不再进入下一层递归。 我们启动了两个线程t1和t2执行同一个递归函数由于std::recursive_timed_mutex的使用即使是在递归调用中这两个线程也能够安全地竞争资源。 同时超时机制确保了在锁竞争激烈的情况下线程不会无限等待。 try_lock_until()用法示例 #include iostream #include mutex #include chrono #include threadstd::recursive_timed_mutex rtmx;void recursive_attempt_to_lock_until(int id, const std::chrono::time_pointstd::chrono::system_clock timeout_time, int depth) {if (rtmx.try_lock_until(timeout_time)) {std::cout Thread id acquired the lock at depth depth std::endl;if (depth 3) {recursive_attempt_to_lock_until(id, timeout_time, depth 1); // 递归尝试锁定}std::this_thread::sleep_for(std::chrono::seconds(1)); // 模拟工作rtmx.unlock();} else {std::cout Thread id failed to acquire the lock at depth depth std::endl;} }int main() {auto timeout_time std::chrono::system_clock::now() std::chrono::seconds(3);std::thread t1(recursive_attempt_to_lock_until, 1, timeout_time, 1);std::thread t2(recursive_attempt_to_lock_until, 2, timeout_time, 1);t1.join();t2.join();return 0; }输出 Thread 1 acquired the lock at depth 1 Thread 1 acquired the lock at depth 2 Thread 1 acquired the lock at depth 3 Thread 2 failed to acquire the lock at depth 1总结 std::recursive_timed_mutex提供了一种灵活的方式来处理需要在同一线程中多次获取锁的情况特别是在递归调用场景中。超时功能也增加了程序的健壮性防止了因等待锁而导致的潜在死锁。
http://www.pierceye.com/news/206027/

相关文章:

  • 在线制作手机网站公司网站建设厂家
  • 在线分析网站一个小外贸公司怎么开
  • 给自己的公司做网站怎么做好电脑手机一体网站
  • 精通网站建设 全能建站密码pdf电商网站设计理念
  • 百度推广建设网站是不是合发手机网站的必要性
  • 企业网站建设是什么实现的物质基础和技术支撑现货交易平台代理
  • 网站建设的描述长沙发布app
  • 好的设计作品网站代理网站建设
  • 做网站的软件m开头网站建设公司问候语
  • 做网站需要工商证吗app软件开发价格
  • 做足球原创短视频网站网站建设永远在路上
  • 做seo为什么要了解网站苏州做网站公司
  • 这几年做哪些网站能致富网站开发账务处理
  • 网站的版权信息做阿里巴巴网站卖货咋样
  • 找项目去哪个网站成都哪里有做网站的公司
  • 网站推广的方法及特点国外专门做童装的网站
  • 企业网站开发模型图wordpress 侧边导航
  • 济南网站系统优化网站建设属于什么专业
  • 114啦建站程序页面效果好的网站
  • 龙华网站建设-信科网络电子商务网站建设和技术现状
  • 网站备案有效期wordpress 评论图片
  • 搭建网站需要哪些步骤wordpress 主题使用
  • 网站怎么发布做微商天眼官方网站
  • qq群网站制作异常网站服务器失去响应
  • aspnet网站开发模板紫光华宇拼音输入法官方下载
  • 东莞网站设计价格wordpress的配置dns
  • 韶关网站建设公司电子商务网站建设考试重点
  • 网站左侧 导航小红书广告投放平台
  • 资阳住房和城乡建设厅网站重庆建设网站建站
  • 网站制作厂家电话多少女生学网络工程难吗