stl 里面的 map 是否自带锁的?

Sandrer 2017-10-24 05:38:31


今天调试程序的时候, 崩在图片上的位置了
发现 map 里面自带锁的?

vs2010 + win7
...全文
308 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2017-10-25
  • 打赏
  • 举报
回复
paschen 版主 2017-10-25
  • 打赏
  • 举报
回复
map标准是没有加锁的,需要进行加锁,代码里有个lock不代表是为了同步访问而去加锁,可能是为了其他需要,也可能是debug下的异常检测,具体取决于代码实现
  • 打赏
  • 举报
回复
应该没有加锁!我以前两个线程同时push_back一个list,结果挂了。 VC2010。 如果默认加锁的话,可能会影响到单线程操作的性能。
FoolCarpe 2017-10-25
  • 打赏
  • 举报
回复
STL标准中未提出map为线程安全 std::map崩溃可能是迭代器访问失效?
走好每一步 2017-10-25
  • 打赏
  • 举报
回复
没加锁的,自己加一下,很简单的。
xskxzr 2017-10-25
  • 打赏
  • 举报
回复
加不加锁,怎么加锁是实现问题。 标准规定容器的线程安全如下:
引用
1. All container functions can be called concurrently by different threads on different containers. More generally, the C++ standard library functions do not read objects accessible by other threads unless those objects are directly or indirectly accessible via the function arguments, including the this pointer. 2. All const member functions can be called concurrently by different threads on the same container. In addition, the member functions begin(), end(), rbegin(), rend(), front(), back(), data(), find(), lower_bound(), upper_bound(), equal_range(), at(), and, except in associative containers, operator[], behave as const for the purposes of thread safety (that is, they can also be called concurrently by different threads on the same container). More generally, the C++ standard library functions do not modify objects unless those objects are accessible, directly or indirectly, via the function's non-const arguments, including the this pointer. 3. Different elements in the same container can be modified concurrently by different threads, except for the elements of std::vector<bool> (for example, a vector of std::future objects can be receiving values from multiple threads). 4. Iterator operations (e.g. incrementing an iterator) read, but do not modify the underlying container, and may be executed concurrently with operations on other iterators on the same container, with the const member functions, or reads from the elements. Container operations that invalidate any iterators modify the container and cannot be executed concurrently with any operations on existing iterators even if those iterators are not invalidated. 5. Elements of the same container can be modified concurrently with those member functions that are not specified to access these elements. More generally, the C++ standard library functions do not read objects indirectly accessible through their arguments (including other elements of a container) except when required by its specification. 6. In any case, container operations (as well as algorithms, or any other C++ standard library functions) may be parallelized internally as long as this does not change the user-visible results (e.g. std::transform may be parallelized, but not std::for_each which is specified to visit each element of a sequence in order)
——http://en.cppreference.com/w/cpp/container
赵4老师 2017-10-25
  • 打赏
  • 举报
回复
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止。 《Windows核心编程》
Sandrer 2017-10-25
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
那为什么程序会崩在加锁的地方??? 崩溃的地方是从 std::map::begin() 函数一直深入进去的

64,683

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧