多线程场景中STL容器(std :: map)的语义 [英] Semantics of STL containers (std::map) in a multithreaded scenario

查看:129
本文介绍了多线程场景中STL容器(std :: map)的语义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我理解C ++标准没有谈论线程。我的

问题更多地针对以某种方式使用STL

容器时会发生什么。我会感激任何想法。我重新讨论我不想探究C ++标准在我从多个线程中抓取一些数据时所说的内容,我只是想知道我是否

已经理解了这一点。


我有一个std :: map< somedatatype,someotherdatatype> myMap",其中东西

定期插入。在多线程环境中,还有

其他代码同时从这张地图中读取。


我想知道我是否理解这一点对。考虑这样的

场景:


//应用程序的一部分中的线程1


busy_inserting_stuff_into_myMap


//在应用程序的另一部分中使用线程2


1.获取对myMap的引用

2.通过执行普通循环开始迭代:


std :: map<> :: iterator itrbegin = myMap.begin();

std: :map<> :: iterator itrend = myMap.end();

std :: map<> :: iterator itr;

for(itr = itrbegin; itr!= itr.end(); ++ itr)

{

}


因为线程1经常插入东西,有没有机会线程2

发现它的所有迭代器在

循环过程中失效?换句话说,从一个线程插入地图

的效果是什么,同时在另一个线程中循环通过相同的地图?我不在乎线程2在

循环期间是否遗漏了某些信息(因为地图一直在更新......)


谢谢!

解决方案



**叹气**


我说:

我重新迭代我不想探究当我从多个线程中抓取一些数据时C ++标准所说的内容,我只是想知道我是否理解这一点。


并最终询问:

换句话说,从一个线程插入地图
同时循环到同一个线程的效果是什么地图在
另一个线程?我不在乎线程2在
循环期间是否遗漏了某些信息(因为地图一直在更新......)




道歉。如果有人仍然想澄清这一点,我会很感激

很多。谢谢!


Dilip schrieb:

由于线程1经常插入东西,是否有机会线程2
发现它的所有迭代器在循环过程中失效了吗?换句话说,从一个线程插入到地图中的效果是什么,同时在另一个线程中循环通过相同的地图?我不在乎线程2在
循环期间是否遗漏了某些信息(因为地图一直在更新......)




< OT>

在其他线程中读取相同的

变量时,不应该在一个线程中写入变量。它不仅仅是使迭代器无效。


写入一个整数时甚至可能遇到问题。

线程A写入变量size,当线程A写入大小的第一个字节时,线程B开始读取它只是

。并留下其他字节

不变。因此,线程B获得了旧的和新的

值大小的字节混合。


访问时使用某种互斥量比一个帖子更多的变量或对象。

< / OT>


Thomas




Thomas J. Gritzan写道:

Dilip schrieb:
< OT>
你不应该写信给一个线程中的变量,同时在其他线程中读取相同的变量。它不仅仅是使迭代器无效。




理解。可悲的是,我处于一个令人尴尬的位置,即将多线程改进到一个重度C ++ / STL依赖的应用程序中,并且我经常会遇到这样的角落情况: - (br />



I understand the C++ standard does not talk about threading. My
question here is directed more towards what happens when a STL
container is used in a certain way. I''d appreciate any thoughts. I
re-iterate I don''t want to probe into what C++ standard says when I
trample some data from multiple threads, I simply want to know if I
have understood this right.

I have a "std::map<somedatatype, someotherdatatype> myMap", where stuff
gets inserted regularly. In a multi-threaded environment, there are
other parts of code that simultaneously read from this map.

I would like to know if I have understood this right. Consider a
scenario like this:

// thread 1 in one part of the application

busy_inserting_stuff_into_myMap

// while thread 2 in another part of the application

1. gets a reference to the myMap
2. starts iterating by performing an ordinary loop:

std::map<>::iterator itrbegin = myMap.begin();
std::map<>::iterator itrend = myMap.end();
std::map<>::iterator itr;
for (itr = itrbegin; itr != itr.end(); ++itr)
{
}

Since thread 1 is regularly inserting stuff, is there a chance thread 2
finds all its iterators invalidated while its in the process of
looping? In other words what is the effect of inserting into a map
from one thread while simultaneously looping through the same map in
another thread? I don''t care if thread 2 misses some info during the
looping (since the map gets updated all the time...)

thanks!

解决方案


**sigh**

I said:

I
re-iterate I don''t want to probe into what C++ standard says when I
trample some data from multiple threads, I simply want to know if I
have understood this right.
and ended up asking:
In other words what is the effect of inserting into a map
from one thread while simultaneously looping through the same map in
another thread? I don''t care if thread 2 misses some info during the
looping (since the map gets updated all the time...)



Apologies. If someone still wants to clarify this, I''d appreciate it a
lot. thanks!


Dilip schrieb:

Since thread 1 is regularly inserting stuff, is there a chance thread 2
finds all its iterators invalidated while its in the process of
looping? In other words what is the effect of inserting into a map
from one thread while simultaneously looping through the same map in
another thread? I don''t care if thread 2 misses some info during the
looping (since the map gets updated all the time...)



<OT>
You should not write to a variable in one thread while reading the same
variable in other threads. Its not just about invalidating iterators.

You could even run into problems when writing to one single integer.
Thread A writes to variable "size", and Thread B starts reading it just
when Thread A wrote the first byte of "size" and left the other bytes
unchanged. So Thread B gets a byte-wise mixture of the old and the new
values of "size".

Use some kind of mutex when you access variables or objects from more
than one thread.
</OT>

Thomas



Thomas J. Gritzan wrote:

Dilip schrieb:
<OT>
You should not write to a variable in one thread while reading the same
variable in other threads. Its not just about invalidating iterators.



Understood. Sadly, I am in an unenviable position of retrofitting
multithreading into a heavily C++/STL dependant application and I
frequently keep running into corner cases like this :-(


这篇关于多线程场景中STL容器(std :: map)的语义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆