C++使用心得
来源:互联网 发布:程序员上的网站 编辑:程序博客网 时间:2024/05/13 13:08
今天关注了下最新的C++标准,并试用了下std::thread,lock_guard等功能,还是不错的。
对于std::thread要join/detach,不然会有异常。
另外当使用socket时网络函数bind应该使用::bind表明是全局的命名空间
#include <iostream>
#include <thread>
#include <chrono>
#include <mutex>
#include <vector>
using namespace std;
int globalCount = 10;
std::mutex g_mutex;
void fun1(int n)
{
std::lock_guard<std::mutex> g(g_mutex);
globalCount++;
n++;
thread::id i = std::this_thread::get_id();
cout << i << " fun1: " << n << endl;
}
void fun2(int& n)
{
n++;
thread::id i = std::this_thread::get_id();
cout << i << " fun1: " << n << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
int n1 = 100;
int n2 = 100;
std::thread t1(fun1, n1);
std::thread t2(fun2, std::ref(n2));
t1.join();
t2.join();
cout << "Main:" << n1 << " , " << n2 << endl;
std::vector<std::thread*> threadList;
for(int i = 0; i < 20; i++)
{
std::thread* t = new std::thread(fun1, i);
threadList.push_back(t);
}
for(int i = 0; i < 20; i++)
{
threadList[i]->join();
}
std::this_thread::sleep_for(std::chrono::seconds(1));
cout << endl << globalCount << endl;
cout << "press any key to exit..." << endl;
char c;
cin >> c;
}
- UltraWinGrid使用心得(C#)
- C#--BackgroundWorker使用心得
- UltraWinGrid使用心得(C#)
- Python C API 使用心得
- axis2/c初次使用心得
- Python C API 使用心得
- c/c++ const使用心得
- Python C API 使用心得
- 一点C#代码的使用心得
- c语言malloc和calloc使用心得
- C语言 clock() 函数使用心得
- C# 中使用OPenCV(Emgu)心得
- C# 中使用OPenCV(Emgu)心得
- C/C++ pthread_cond_timedwait()函数使用心得
- .c心得
- Excel对象模型的一些使用心得(C#)
- C/C++使用心得:enum与int的相互转换
- mysql-connector-c++-noinstall-1.0.5-win32-vs2005 使用心得
- const violate mutuable
- LeetCode 124: Binary Tree Maximum Path Sum
- Linux命令之stty - 显示和修改终端行设置
- java调试整理
- 新开了一个Octpress博客
- C++使用心得
- Python 的应用
- 2013 ACM/ICPC Asia Regional Online —— Warmup 1002
- 调用ITelephony的endCall( )方法自动挂断电话
- java从键盘获取数值的三种方法
- Sicily 1036. Crypto Columns
- CHM 帮助文档制作
- ubuntu下解决SD卡只读的问题
- 关于随机数的一点新的小发现