multithreading - Thread-safe C++ stack -


I am new to C ++ and writing a multi-threaded app that lets different authors get stack and readers Objects have to be pushed. By stacking them (or pushing the pointer to at least one object) ..

Are any of the structures built in C ++ without adding locking code etc? If not, what about the boot libraries?

Edit:

Hi. Thanks to the initial great answers, I think one of the reasons I thought it could be made is that I was completely thinking in x86 space and thinking that a posh / pop pointers would have a nuclear action at the instruction level needed.

I am not sure whether my initial hump is true or not, but I think it is not necessary that it is true in all platforms. However, if you are running on x86, do you get nuclear push and POP on the stack, and if this is what makes it essentially lock-free?

Yes: Great, and your needs should fit very well. (These days, many people say that you can increase the efficiency of almost Boost.)

There is still no class that you can use the out-of-box, but once You have the synchronization hand at the earliest, it is actually quite easy to implement your thread-safe cover, for example, std :: stack this may look something like this (apply to each method Not doing ...):

  template & lt; Typename T & gt; Class MyThreadSafeStack {Public: Zero Push (const T & amp; Item) {boost :: mutex :: scoped_lock lock (m_mutex); M_stack.push (item); } Zero pop () {boost :: mutex :: scoped_lock lock (m_mutex); M_stack.pop (); } T top () const {// Note that we should not return a reference, // because to promote any other thread in the meantime (//) for this purpose: / mutex :: scoped_lock lock (m_mutex ); Return m_stack.top (); } Private: shaky boost :: mute x m_mutex; Std :: pile & lt; T & gt; M_stack; }  

If you are new to C ++, please know about this. Regarding the matter, Boost. Forgetting to leave the lock and make it difficult to shoot yourself in the foot, there are "screw lock" classes.

If you ever write code in this way:

  Zero Dastf () {myLock.lock (); If (! Condition) {reportError (); MyLock.unlock (); Return; } Try {doStuffThatMayThrow (); } Hold (std :: exception & amp; e) {myLock.unlock (); Throw e } DoMoreStuff (); MyLock.unlock (); }  

, you should not just say and instead should go to RAI (not directly with syntax boost):

  zero doaster () {scoped_lock lock ; If (! Condition) {reportError (); Return; } DoStuffThatMayThrow (); DoMoreStuff (); }  

The issue is that when the scoped_lock object exits out of the scope, its destroyers release the resource - in this situation, the lock would always be It does not matter that you get out of throwing an exception, or by using the strange return statement, by adding your partner to the middle of your work, or reaching the end of the bus, the function.


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -