允许超时的互斥量
来源:互联网 发布:windows好用的v翻墙 编辑:程序博客网 时间:2024/05/17 03:47
#include <windows.h>
#include <iostream>
#include <thread>
#include <string>
#include <mutex>
using namespace std;
timed_mutex g_timed_mutex;
void thread1()
{
unique_lock<timed_mutex> t1(g_timed_mutex);//lock_guard只是提供了对互斥量最基本的加解锁封装,而unique_lock提供了多种构造方法,使用起来更加灵活,对于允许超时的互斥量需要使用unnique_lock来包装。
::Sleep(3000);//睡眠3秒
puts("thread1");
}
void thread2()
{
unique_lock<timed_mutex> t1(g_timed_mutex, chrono::milliseconds(1000));//超出时间1秒
puts("thread2");
}
int main(int argc, char* argv[])
{
thread t1(thread1);
Sleep(100);//让线程1先启动
thread t2(thread2);//如果线程1对共享资源的访问时间比较长,这时线程2可能等不了那么久,所以设置一个超时时间,在超时时间内如果线程1中的互斥量还没有解锁,线程2就不等了,继续往下执行。
t1.join();
t2.join();
system("pause");
return 0;
}
#include <iostream>
#include <thread>
#include <string>
#include <mutex>
using namespace std;
timed_mutex g_timed_mutex;
void thread1()
{
unique_lock<timed_mutex> t1(g_timed_mutex);//lock_guard只是提供了对互斥量最基本的加解锁封装,而unique_lock提供了多种构造方法,使用起来更加灵活,对于允许超时的互斥量需要使用unnique_lock来包装。
::Sleep(3000);//睡眠3秒
puts("thread1");
}
void thread2()
{
unique_lock<timed_mutex> t1(g_timed_mutex, chrono::milliseconds(1000));//超出时间1秒
puts("thread2");
}
int main(int argc, char* argv[])
{
thread t1(thread1);
Sleep(100);//让线程1先启动
thread t2(thread2);//如果线程1对共享资源的访问时间比较长,这时线程2可能等不了那么久,所以设置一个超时时间,在超时时间内如果线程1中的互斥量还没有解锁,线程2就不等了,继续往下执行。
t1.join();
t2.join();
system("pause");
return 0;
}
0 0
- 允许超时的互斥量
- 更改SAP系统超时断线所允许的最大时间
- 更改SAP系统超时断线所允许的最大时间
- 更改SAP系统超时断线所允许的最大时间 (转载)
- 如何在允许超时时间内接受来自控制台输入的指定指令
- 允许并列的排名
- switch()允许的数据类型
- Socket的读取超时与连接超时
- HttpClient的请求超时和响应超时
- IIS6的超时问题
- NIO的超时处理
- SquirrelMail超时的问题
- WebService超时的解决办法
- poplib的超时问题
- 简单的超时设置
- nginx的超时处理
- 数据库连接超时的问题
- connect的超时设置
- 子贡赎人
- C Looooops poj 2115 扩展欧几里得
- OKHttp源码解析
- HTTP工作流程
- OkHttp使用教程
- 允许超时的互斥量
- 常用Maven收集以及Maven技巧
- mybatis 批量insert,update
- tableView分组 头部悬停
- std::map 删除指定条件的元素
- hadoop系列文档3-配置Hdfs高可用HA
- Oracle回收站操作
- C++复习要点总结(一)
- 【java框架-复习spring】spring中各个模块的作用