本文介绍了std :: deque Thread Saftey Situtation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我在网上看到有两个写入不安全,我理解,但是1个线程push()'和1个线程pop()''是 线程安全吗?基本上我的情况如下: - 线程1-- 1.读取TCPIP缓冲区 2.添加缓冲区到队列(q.size()检查队列是不是已满,并且 q.push_back(...)) 3.信号读取线程事件和放大器;返回等待更多消息 TCPIP上的 - 线程2 - 1.等待信号 2. while(q.size()0)myStruct = q.front(); 2a。处理myStruct(没有修改它,但会为另一个队列复制一些 信息)。 2b。删除[] myStruct.Buffer 2c。 q.pop_front(); 3.回去等待单个 我已经运行了几个小时的应用程序(基本上使TCPIP饱和) 流量)并没有明显的问题。 流程 myStruct需要一段时间,我无法在处理工作时锁定push_back(...)线程 。 我可以添加关键部分锁定围绕.size(),。front()和/ 或.pop_front()/。push_back(),但我的第一个倾向是这个 是线程安全的吗? 我担心的是,如果一个.push_back()在一个带有1个节点 的双端队列中开始。它看到第一个节点并修改其中的某些内容以指向要添加的新节点,然后是.pop_front()。 发生了这种情况。我不知道队列如何实现 内部因此不确定这是否是一个有效的担心。性能非常重要而且我绝对不会阻止,除非它需要 这就是我在这里问的原因:) 如果需要关键部分,它是否只是前面的 " .pop_front()/。push_back()"?或者我正在使用的所有成员函数 (.size()/。front())。 谢谢。任何信息将不胜感激。这些只是两个线程/函数访问队列的 。我目前 实现了锁定所有只是为了安全,但是想 删除锁定,如果不需要,或微调它。I''ve read a bit online seeing that two writes are not safe, which Iunderstand, but would 1 thread push()''ing and 1 thread pop()''ing bethread-safe? Basically my situation is the follows:--Thread 1--1. Reads TCPIP Buffer2. Adds Buffer to Queue (q.size() to check queue isn''t full, andq.push_back(...))3. Signals Reading Thread Event & Goes Back to Wait for More Messageson TCPIP--Thread 2--1. Waits for Signal2. while(q.size() 0) myStruct = q.front();2a. processes myStruct (doesn''t modify it but does copy someinformation for another queue).2b. delete[] myStruct.Buffer2c. q.pop_front();3. Goes back to waiting for singleI''ve run the app for a few hours (basically saturating the TCPIPtraffic) and there were no apparent problems. The "processesmyStruct" takes a while and I can''t have the push_back(...) threadlocked while processing is working.I can add Critical Section locks around the ".size()", ".front()" and/or ".pop_front()/.push_back()", but my first inclination is that thisis thread safe?My worry is that say if a .push_back() starts on a deque with 1 nodein it. It sees the 1st node and modifies something in it to point tothe, to be added, new node, and then the ".pop_front()" occurs whilethat?s happening. I have no idea how the queue is implementedinternally so unsure if this is a valid worry. Performance is veryimportant and I would rather absolutely no blocking unless it''s neededwhich is why I ask here :)If Critical Sections are needed, would it just be fore the".pop_front()/.push_back()"? or all member functions I''m using(.size()/.front()).Thanks. Any information would be greatly appreciated. These are theonly two threads/functions accessing the queue. I currentlyimplemented it with locking on all just to be safe, but would like toremove the locking if it is not needed, or fine tune it.推荐答案 否No 如果没有失败,那你很幸运。有一天你肯定会用完 运气。If it did not fail, you were simply lucky. You will surely run out ofluck some day. 只有当你使用互斥锁保护所有东西时才是线程安全的( 很可能是一个windows CRITICAL_SECTION)。 /> [snip]It is thread-safe only when you protect everything with a mutex (whichcould very well be a windows CRITICAL_SECTION).[snip] 所有这些。On all of them. 您是否在没有锁定的情况下衡量性能?据推测,它会不会产生什么影响。 / PeterDid you measure the performance without a lock? Presumably, it wouldnot really make a difference./Peter 干净的锁定方式IE lock(); while(q.size ()0){ 处理((。front()。缓冲区); 删除[]等 ..pop_front(); } unlock(); 减速方式(因为Process(..))在while循环中移动锁定 ,并添加一些tmp变量,并且还管理 " q.size()&虽然循环让代码变得更加丑陋,但我相信表现并不是太糟糕;只是看起来好像我想要增加性能,解决 锁定/解锁在while循环中可能是一件好事 来研究(while循环可以运行数百甚至数千 次,取决于其他线程抽出队列的速度)。 谢谢The clean locking way IElock();while(q.size() 0) {Process((.front().Buffer);delete[] etc..pop_front();}unlock();Was way to slow (because of the "Process(..)"). Moving the lockinside the while loop, and adding some tmp vars, and also managing the"q.size()" in the head of the while loop made the code a little moreugly, but I believe the performance isn''t too much worst; just seemedto me that if I wanted to increase the performance, tackling thelocking/unlocking that''s inside the while loop might be a good thingto look into (the while loop can run hundreds, maybe thousands oftimes depending how fast the other thread is pumping the queue up).Thanks 干净的锁定方式IE lock(); while(q.size ()0){ 处理((。前()缓冲区); 删除[]等 .pop_front();} unlock();The clean locking way IElock();while(q.size() 0) {Process((.front().Buffer);delete[] etc.pop_front();}unlock(); 这一点都不干净。你在处理很多元素时锁定, 虽然你只需要锁定你持续弹出/推动的短暂时间 元素。 搜索条件变量关于如何做这种 的想法。生产者 - 消费者结构非常基本,而且大多数有用 - 但它必须正确实施。This is not at all clean. You lock while processing many elements,while you only should lock for the short duration where you pop/pushan element.Search for "condition variables" for an idea on how to do this kind ofstuff. A producer-consumer structure is really quite basic and mostuseful - but it must be implemented correctly. 低级互斥是一种非常快速的野兽,除非你有很大的争论。如果您的数据复制成本很高,那可能是一个瓶颈,但等待处理,直到您的 基本结构工作为止。 / PeterA low level mutex is a very fast beast unless you have heavycontention. If your data is expensive to copy, that might be abottleneck, but wait dealing with that until you have gotten yourbasic structure to work./Peter 这篇关于std :: deque Thread Saftey Situtation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-05 08:21