multithreading - Implementing a thread-safe, generic stack in C++ on linux -


In a recent interview, I apply the thread to the normal (ietemplate-based) stack on the Linux machine in C ++ Was said to be.
I quickly came up with the following (there may be compilation errors).
I got it The interviewer might have liked something in this implementation. Maybe the part of the design :) / Here are some of the problems that can be in this implementation: -
1. Improved implementation to indicate overflow / underflow I am using STL vector in the form of the underlying data structure, so there is no overflow management. Should I have any such handle? In addition, the underflow (in pop ()) produces false as the return value. Should it be done by throwing an exception?
2. Implementation of Popemy Regularity Is the following implementation correct ?? 3. There is no real use of the top element.
4. Better time between the beginning of the author and the reader thread.

Please make any comments / suggestions / corrections. Thank you.

// Implement a thread secure generic stack.

  # include & lt; Pthread.h & gt; # Include & lt; Iostream & gt; #to & lt include, vector & gt; using namespace std; Template & lt; Typename T & gt; Class MyStacks {Public: // Interface Bull Push (TA AM); Bull pop (T & AM); Bull IePT (); // Constructor Mistake () {pthread_mutex_init (and lock); Top = 0; } // destructor ~ MyStack () {pthread_mutex_destroy (& lock); } Private: pthread_mutex_t lock; Int top; Of vector & lt; T & gt; Pile; Bull Mystack :: Push (T-Ames); Bull Mystack :: Popelem (T & AM); }; // End of MyStack template & lt; Typename T & gt; Bull Mystique & lt; T & gt; :: Push (T Ames) {pthread_mutex_lock (& ​​lock); PushElem (ELEM); Pthread_mutex_unlock (& ​​amp; lock); } Template & lt; Typename T & gt; Bull Mystack & lt; T & gt; :: pop (T & amp) {pthread_mutex_lock (& ​​amp; lock); PopElem (ELEM); Pthread_mutex_unlock (& ​​amp; lock); } Template & lt; Typename T & gt; Bull Mystique & lt; T & gt; :: Pushlem (t AMM) {stack.push_back (AMM); Top = stack size (); } Template & lt; Typename T & gt; Bull Mystique & lt; T & gt;: PopElem (T & amp; elem) {if (this. ISFT)} {return false; } Elem = stack.back (); // hard, final element stack.pop_back (); // Is valid valid after this ?? Top = stack size (); Back true; } Template & lt; Typename T & gt; Bull Mystique & lt; T & gt; :: IsEmpty () {return stack.empty (); } Class MyStackTest {Public: Zero Start () {pthread_init (& readerT); Pthread_init (& writerT); } Run zero () {pthread_create (authorship, 0, author, 0); Pthread_create (readerT, 0, reader, 0); Pthread_join (& writerT); Pthread_join (in & amp; readerT); Private: pthread_t readerT; Pthread_t Author T; MyStack & LT; Integer & gt; Pile; Zero reader (zero); Zero author (zero); }; Zero MyStackTest :: Author () {for (int i = 0; i & lt; 20; i ++) {stack.ush (i); Cout & lt; & Lt; "\ N \ t Push Element:" & lt; & Lt; I; } //} for my} zero MyStackTest :: reader () {int elem; While (stack.pump (AMM)) {cout & lt; & Lt; "\ N \ t Popped in:" & lt; & Lt; Elem; }} Int main () {MyStick Test Test; test run(); }  

Some issues:

  • I will apply A locker class claiming & amp;
  • I will use the std :: stack
  • I will use the locker to apply the locking policy to the std :: stack user - stack locks themselves are bad designs, Because the stack can not know how to use it

Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -