#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

最新文章

  1. css3的一些新属性1
  2. RecycleView使用的那些坑
  3. ChainOfResponsibility
  4. news总结
  5. REDGATE又一好用的脚本工具ScriptsManager1.3
  6. [Firewall] iptables Configuration
  7. 百度前端技术学院js任务三
  8. 360大牛:全面解读PHP面试
  9. Linux 上使用LVM 扩展磁盘Size
  10. 配置使用 git 秘钥连接 GitHub
  11. dubbo超时优先级设置
  12. PHP swoole process的使用
  13. FFmpeg(14)-使用NDK、C++完成EGL,display, surface, context的配置和初始化
  14. windows8 Metro App用Javascript来调用C#的library
  15. 02 请求库之 requests模块
  16. pat04-树5. File Transfer (25)
  17. 《web-Mail服务的搭建》
  18. day 16 JS DOM 继续
  19. 使用superobject 新建Json数据(数组)
  20. 关于SpringMVC注解

热门文章

  1. C#基础拾遗系列之二:使用ILSpy探索C#7.0新增功能点
  2. Java中的深拷贝和浅拷贝(转载)
  3. Codeforces Round #415 (Div. 2) C. Do you want a date?
  4. 09_Redis持久化——AOF方式
  5. less @import and extend及mixin详解
  6. Linux -&gt;&gt; scp命令复制对端机器上的文件/文件夹
  7. MYSQL连接相关参数和状态值详解
  8. SVN常用功能介绍(二)
  9. July 17th 2017 Week 29th Monday
  10. React Native for Android on Windows 配置开发安装总结