lock free
2024-09-25 16:28:27
#include <thread>
#include <iostream>
#include <mutex>
#include <atomic>
#include <chrono> using namespace std; #ifdef _LOCK_FREE atomic<int> i = ; void foo() {
int count = ;
while (count--)
{
i++;
}
} int main()
{
chrono::steady_clock::time_point start_time = chrono::steady_clock::now(); thread thread1(foo);
thread thread2(foo); thread1.join();
thread2.join(); cout << "i = " << i << endl; chrono::steady_clock::time_point end_time = chrono::steady_clock::now();
chrono::duration<double> time_span = chrono::duration_cast<chrono::microseconds>(end_time - start_time);
cout << "lock free elpased time: " << time_span.count() << " ms" << endl; system("pause"); return ;
}
#endif // _LOCK_FREE #ifdef _MUTEX_LOCK
int i = ;
mutex g_mutex; void foo() {
int count = ;
while (count--)
{
g_mutex.lock();
i++;
g_mutex.unlock();
}
} int main()
{
chrono::steady_clock::time_point start_time = chrono::steady_clock::now(); thread thread1(foo);
thread thread2(foo); thread1.join();
thread2.join(); cout << "i = " << i << endl; chrono::steady_clock::time_point end_time = chrono::steady_clock::now();
chrono::duration<double> time_span = chrono::duration_cast<chrono::microseconds>(end_time - start_time);
cout << "mutex lock elpased time: " << time_span.count() << " ms" << endl; system("pause"); return ;
}
#endif _MUTEX_LOCK
最新文章
- css3的一些新属性1
- RecycleView使用的那些坑
- ChainOfResponsibility
- news总结
- REDGATE又一好用的脚本工具ScriptsManager1.3
- [Firewall] iptables Configuration
- 百度前端技术学院js任务三
- 360大牛:全面解读PHP面试
- Linux 上使用LVM 扩展磁盘Size
- 配置使用 git 秘钥连接 GitHub
- dubbo超时优先级设置
- PHP swoole process的使用
- FFmpeg(14)-使用NDK、C++完成EGL,display, surface, context的配置和初始化
- windows8 Metro App用Javascript来调用C#的library
- 02 请求库之 requests模块
- pat04-树5. File Transfer (25)
- 《web-Mail服务的搭建》
- day 16 JS DOM 继续
- 使用superobject 新建Json数据(数组)
- 关于SpringMVC注解
热门文章
- C#基础拾遗系列之二:使用ILSpy探索C#7.0新增功能点
- Java中的深拷贝和浅拷贝(转载)
- Codeforces Round #415 (Div. 2) C. Do you want a date?
- 09_Redis持久化——AOF方式
- less @import and extend及mixin详解
- Linux ->;>; scp命令复制对端机器上的文件/文件夹
- MYSQL连接相关参数和状态值详解
- SVN常用功能介绍(二)
- July 17th 2017 Week 29th Monday
- React Native for Android on Windows 配置开发安装总结