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

模板网站搭建网站做缓存

模板网站搭建,网站做缓存,phpcms做企业网站授权,游戏公司错误原因分析与解决方法 错误原因 错误C2039:“try_lock_until”:不是"std::mutex"的成员的根本原因是std::mutex类型不支持try_lock_until方法。try_lock_until是std::timed_mutex或std::recursive_timed_mutex的成员函数,而不是std::mutex的成员函数。…

错误原因分析与解决方法

错误原因

错误C2039:“try_lock_until”:不是"std::mutex"的成员的根本原因是std::mutex类型不支持try_lock_until方法。try_lock_untilstd::timed_mutexstd::recursive_timed_mutex的成员函数,而不是std::mutex的成员函数。因此,当尝试在std::mutex上调用try_lock_until时,编译器会报错,提示找不到该成员函数。

错误代码

以下是原始代码示例,展示了错误的使用方式:

#include <iostream>
#include <thread>
#include <mutex>
#include <chrono>std::mutex mtx;void worker() {std::this_thread::sleep_for(std::chrono::seconds(2));std::lock_guard<std::mutex> lock(mtx);std::cout << "Worker thread locked the mutex." << std::endl;std::this_thread::sleep_for(std::chrono::seconds(2));std::cout << "Worker thread unlocked the mutex." << std::endl;
}int main() {std::thread t(worker);std::unique_lock<std::mutex> lock(mtx, std::defer_lock);auto timeout = std::chrono::steady_clock::now() + std::chrono::seconds(1);if (lock.try_lock_until(timeout)) { // 错误:std::mutex 不支持 try_lock_untilstd::cout << "Main thread locked the mutex." << std::endl;lock.unlock();}else {std::cout << "Main thread failed to lock the mutex within the timeout." << std::endl;}t.join();return 0;
}

错误提示

编译时会报错:

error C2039: "try_lock_until": 不是 "std::mutex" 的成员
message : 参见“std::mutex”的声明
message : 查看对正在编译的函数模板实例化“bool std::unique_lock<std::mutex>::try_lock_until<std::chrono::steady_clock,std::chrono::nanoseconds>(const std::chrono::time_point<std::chrono::steady_clock,std::chrono::nanoseconds> &)”的引用
message : 请参阅 "main" 中对 "std::unique_lock<std::mutex>::try_lock_until" 的第一个引用

正确代码

要解决此问题,需要将std::mutex替换为std::timed_mutex,因为std::timed_mutex支持try_lock_until方法。以下是修改后的正确代码:

#include <iostream>
#include <thread>
#include <mutex>
#include <chrono>std::timed_mutex mtx;
bool ready = false;void worker() {std::this_thread::sleep_for(std::chrono::seconds(2));std::lock_guard<std::timed_mutex> lock(mtx); // 使用 std::timed_mutexstd::cout << "Worker thread locked the mutex." << std::endl;std::this_thread::sleep_for(std::chrono::seconds(2));ready = true;std::cout << "Worker thread unlocked the mutex." << std::endl;
}int main() {std::thread t(worker);std::unique_lock<std::timed_mutex> lock(mtx, std::defer_lock); // 使用 std::timed_mutexauto timeout = std::chrono::system_clock::now() + std::chrono::seconds(1);if (lock.try_lock_until(timeout)) { // 正确:std::timed_mutex 支持 try_lock_untilstd::cout << "Main thread locked the mutex." << std::endl;lock.unlock();}else {std::cout << "Main thread failed to lock the mutex within the timeout." << std::endl;}t.join();return 0;
}

注意事项

• 选择合适的互斥锁类型

• 如果需要支持超时锁定功能(如try_lock_untiltry_lock_for),应使用std::timed_mutexstd::recursive_timed_mutex

• 如果不需要超时功能,可以继续使用std::mutexstd::recursive_mutex

• 确保编译器支持

try_lock_until是 C++14 标准引入的功能。确保你的编译器支持 C++14 或更高版本。在 Visual Studio 2022 中,默认支持 C++14。

• 头文件包含

• 确保包含<mutex><chrono>头文件:

     #include <mutex>#include <chrono>```通过以上修改,可以解决编译错误并正确实现超时锁定功能。---希望这个润色后的版本对你有帮助!

文章转载自:

http://J97cxMNI.jsLzh.cn
http://7aV3jd9E.jsLzh.cn
http://36zfR709.jsLzh.cn
http://EgRtztAq.jsLzh.cn
http://qmN7o0HA.jsLzh.cn
http://eG3TZhjK.jsLzh.cn
http://ypbPaTFN.jsLzh.cn
http://pLjPV8VI.jsLzh.cn
http://s2oWa73K.jsLzh.cn
http://eWtROYh0.jsLzh.cn
http://tWvG3Sss.jsLzh.cn
http://2mA89PiU.jsLzh.cn
http://xPYBUFeM.jsLzh.cn
http://iOepi3qd.jsLzh.cn
http://fv2xztnw.jsLzh.cn
http://95igBM33.jsLzh.cn
http://DOTZ8ARz.jsLzh.cn
http://RRGUCatS.jsLzh.cn
http://1S5BZue8.jsLzh.cn
http://qImCSl9m.jsLzh.cn
http://Oz3PwMpg.jsLzh.cn
http://nXOXHaaQ.jsLzh.cn
http://aCFzPFDe.jsLzh.cn
http://WxXYH5uy.jsLzh.cn
http://5Z3cTydh.jsLzh.cn
http://LbBZHyKe.jsLzh.cn
http://w7Pfwnvi.jsLzh.cn
http://RSSNQ3LG.jsLzh.cn
http://5uPNA01i.jsLzh.cn
http://nSV1Rl42.jsLzh.cn
http://www.dtcms.com/wzjs/708824.html

相关文章:

  • WordPress报价表北京seo网络推广
  • 海南房产网站开发建筑工程公司宣传册设计样本
  • 做系统网站信息检索网站佛山哪里做网站
  • 上高做网站公司动漫设计专业学校
  • 东莞专业设计网站知晓程序网站怎么做
  • 江西南昌网站建设公司哪家好关于公司网站建设的请示
  • 公司做的网站如何开启伪静态ui在线设计网站
  • 做类似昵图网网站作风建设提升年活动网站
  • 学校网站群建设方案星月教你做网站的文档
  • 赣州网站推广哪家最专业重庆渝兴建设有限公司网站
  • 广州省建设监理协会网站深圳防疫最新进展
  • 福州网站制作好的企业网站建设盒子怎么搭建
  • 摄影网站建立网站建设必要性和意义
  • 高密市建设局网站表白网站在线制作软件
  • 电影资源网站怎么做的手表之家官网
  • 福州做网站优化电商运营培训班
  • 北京网站建设公司网站优化资讯百科网站推广
  • 建设一个网站app需要多少钱wordpress的搭建教程 pdf
  • 网站域名优势自己建设个人网站要花费多少
  • 苏州建设局统计网站网易企业邮箱密码忘记了怎么找回密码
  • 网站排名套餐互联网站管理工作细则
  • 秀网站模板小游戏入口免费游戏
  • 电商网站设计论文图片加文字在线制作
  • 专业做消防工程师的正规网站网店推广有哪些
  • 合肥高端网站建设费用安卓开发工具下载
  • 哪个网站学习做辅助 天堂资源官网在线资源
  • 网站风格配置怎么做网站关闭模板
  • iis7.5网站配置网络服务费要交印花税吗
  • 网站多级栏目wordpress过滤敏感
  • 天水市建设银行官方网站网络营销品牌策划优化