UNIX(多线程):24---哪些STL容器是线程安全的
生活随笔
收集整理的這篇文章主要介紹了
UNIX(多线程):24---哪些STL容器是线程安全的
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在日常C++開發(fā),少不了和STL,多線程打交道,那么在多線程下,哪些容器時(shí)線程安全的,那些不是?
好,這里就舉一個(gè)簡單的例子,我們先看看vector容器,相信你能看出一些問題:
#include <iostream> #include <string> #include <algorithm> #include <thread> #include <vector>using namespace std; vector<int> vec; void myprint(void) {while(1){for(auto it = vec.begin(); it != vec.end(); it++){*it = 65535;cout << "輸出:"<< *it<< endl;}vec.push_back(1);vec.push_back(1);vec.push_back(1);cout<<"插入數(shù)據(jù)"<<endl; // sleep(1);}return;} int main() {vec.push_back(0);thread my_threads(myprint);while(true){vec.push_back(1);vec.clear()總結(jié)
以上是生活随笔為你收集整理的UNIX(多线程):24---哪些STL容器是线程安全的的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: online游戏服务器架构--网络架构
- 下一篇: 程序以及论文