在日常C++开发,少不了和STL,多线程打交道,那么在多线程下,哪些容器时线程安全的,那些不是?
好,这里就举一个简单的例子,我们先看看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<<"插入数据"<<endl;
// sleep(1);}return;}
int main()
{vec.push_back(0);thread my_threads(myprint);while(true){vec.push_back(1);vec.clear()