Implementation of a stack supporting multi-threaded synchronization

xiaoxiao2021-03-06  50

The stack element is an uncontrolled character array (ie bytes array). Only the address of the array is stored in the stack. This address points to a storage area, the structure of the storage area: ________________________________________________________________________________________________________________________________________________________________________________________________________________ --------------------------------------------- This stack supports more Thread synchronization operations, when the elements of the stack are changed, there is a mutex MUTEX to prevent disagreement. Working environment: Linux 9.0 compile: g bytestack.cpp main.cpp -o main -lpthread The following is the code section: two main.cpp functions, the previous one is a general single-thread application. The latter is a multi-threaded application. / * Bytesstack.h zhangggdlt 2004/11/15 to realize a stack storing bytes array. * / # IFNDef _BYTES_STACK_H # define _bytes_stack_h

#include #include

#define operation_ok 0 # define stack_full -1 # define stack_empty -2 # Define Increase_failed -3 # define no_Area -4 # define point_null -5 # define failed_lock -6

Typedef int err_number; typedef unsigned char uint_8;

/ * The class BytesStack is used to realize store an unsigned char array into the stack which sustain mutiple thread and sycronization. The size of the queue can be set when it is constructed and you can also increas the size of the queue during the application. * / class BytesStack {private: int _size; int _top; int _bottom; uint_8 ** _ buffer; pthread_mutex_t StaMutex; public: BytesStack (int size = 512); ERR_NUMBER increaseSize (int size = 512); ERR_NUMBER push (const uint_8 * data , int LEN); err_number pop (uint_8 * data, int & len); void destroy (); void errMessage; Void ShowbytesTack (BYTESSTACK & BS);

#endif // _ bytesstack_h

----------------------------- / * bytesstack.cpp zhangggdlt 2004/11/15 to realize a stack storing bytes Array Which Sustain the mutitread and sycronization. * / # include #include #include "BytesStack.h" / * The constructor. This BytesStack can sustain sycronization among the mutiThread. It means you can use this data structure Under mutithread. * / bytesstack :: bytesstack (int size) // size = 10 {this -> _ size = size; this -> _ buffer = new (uint_8 *) [this -> _ size]; this -> _ TOP = 0; this -> _ bottom = 0; pthread_mutex_init (& staMutex, null);}

/ * You can use this number fuction to increase the size of the stack The data will not be lost during the increasement * / ERR_NUMBER BytesStack :: increaseSize (int size) {uint_8 ** temp; int i;.. This -> _ size = size; if (! (Temp = new (uint_8 *) [this -> _ size])) Return Increase_failed; for (i = 0; i _ top; i ) {temp [i] = this-> _buffer [i];} delete [] this -> _ buffer; this -> _ buffer = temp; return operation_ok;}

/ * This function is use to accept one element into the stack. You must remember the element is a unsigned char array. Len is the length of the data. * / ERR_NUMBER BytesStack :: push (const uint_8 * data, int len) { UINT_8 * TEMP; if (PTHREAD_MUTEX_TRYLOCK (& staMutex)) {Printf ("Try Lock Failed! / N"); Return Faled_lock;} if (this -> _ TOP> = this -> _ size) {printf ("The Stack is Full! / n "); pthread_mutex_unlock (& ​​StaMutex); return STACK_FULL;} if ((temp = new uint_8 [len 4]!)) {pthread_mutex_unlock (& ​​StaMutex); return NO_AREA;} this -> _ buffer [this -> _ top] = Temp; memcpy (this -> _ buffer [this -> _ top], & len, 4); memcpy (this -> _ buffer [this -> _ top] 4, data, len); this -> _ top ; pthread_mutex_unlock (& ​​staMutex); return OPERATION_OK;} / * This function is use to set free one element from the stack you must get a buffer big enough to store the data before you call the function At the same time you need a more int & len to get the data.. Length. * / err_number bytesstack :: POP (uint_8 * data, int & len) {ified (PTHR ead_mutex_trylock (& ​​StaMutex)) {printf ( "Try lock failed / n!"); return FAILED_LOCK;} if (data) {pthread_mutex_unlock (& ​​StaMutex); return POINT_NULL;} if (this -> _ top <= this -> _ bottom)! {Printf ("THE STACK IS Empty! / N"; pthread_mutex_unlock (& ​​stamutex); return stack_empty;} this -> _ top -; memcpy (void *) & len, this -> _ buffer [this -> _ top], 4 ); Memcpy ((void *) Data, this -> _ buffer [this -> _ TOP] 4, LEN); pthread_mutex_unlock (& ​​stamutex); return operation_ok;}

/ * This function is use to set free the data structure. * / Vid bytesstack :: destroy () {while (this -> _ top> this -> _ bottom) {delete [] (this -> _ buffer [- (this- > _TOP)]);} delete [] (this -> _> _ buffer; this -> _ size = 0; this -> _ buffer = null;} / * this fuction is use to test. show the result of the call fuction. * / VOID BYTESSTACK :: ErrMessage (Err_Number Err) {Switch (ERR) {Case Operation_ok: Printf ("Push IS OK! / N"); Break; Case Stack_Full: Printf ("Push Failed! The Stack Is Full !! / N "); Break; Case Stack_empty: Printf (" Pop Failed! The Stack IS Empty !! / N "); Break; Case Increase_Failed: Printf (" Increase Stack Size Failed! / N "); Break; Default: Printf (" Other Things Are Wrong! / N "); Break;}}

/ * This fuction is buy to show the infomation of the current queue. * / Vid bytesstack :: showbytestack (bytesstack & bs) {printf ("% s / n", "the info of the bytesstack is:"); printf (" Size:% D / N ", BS._SIZE); Printf (" TOP:% D / N ", BS._TOP); Printf (" Bottom:% D / N ", BS._BOTTOM); Printf (" BUF ADDR : 0x% x / n ", bs._buffer);

/ * Using Namespace NetworkProtocols;

// this is a good example to show how to use the the data structure bytesqueue.

INT main () {Int Len, I; char ch; err_number err; uint_8 bufi [] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; uint_8 bufo [10]; bytesstack BS; BS.Showbytesstack (BS); ch = getchar (); while (ch! = 'q') {switch (ch) {case 'i': err = bs.push (bufi, 10); bs.ERRMESSAGE Err); BS.Showbytesstack (BS); CH = getcha (); Break; Case 'o': err = bs.pop (bufo, len); bs. hydrmessage (err); BS.SHOWBYTESSTACK (BS); CH = GetChar (); Break; Case 'E': Err = BS.IncreaseSize (); BS.RrMessage (ERR); BS.Showbytesstack (BS); CH = getchar (); Break; Case 'h': Printf (". ................... Help ..................... "); Printf (" i: push an array Into Stack./N "); Printf (" o: pop an arch "; printf (" e: enlarge the size of the stack./N "); Printf (" H: Help / N) "); Printf (" Q: Quit the system./n "); ch = getchar (); break; default: if (ch! = '/ N') Printf (" ........ .Your Input is Wrong! Again! .......... ../n "); ch = getchar (); break;}} bs.destroy (); bs.showbytesstack (bs); return 0;} * / -------------- ---------------- // main.cpp # include #include "bytesstack.h" typedef struct {int id; BYTESSTACK * BS; uint_8 * buf; int INT delay;} myparameter;

PThread_t threads [5]; pthread_mutex_t quemX; pthread_attr_ttt;

Void * push (void * pvar) {INT i = 1; MyParameter * Para = (MyParameter *) PVAR; While (i) {printf ("Thread Inque:% D Is Working! / N", PARA-> ID); // Para-> BQ-> Inqueue (Para-> BUF, Para-> LEN); // Para-> BQ-> showbytesqueue (* (Para-> BQ)); Para-> BS-> Push (PARA- > Buf, Para-> LEN); Para-> BS-> Showbytesstack (* (Para-> BS)); Usleep (Para-> Delay); i ;} pthread_exit (null);} void * pop (void * pvar) {INT i = 1; MyParameter * Para = (MyParameter *) PVAR; while (i) {printf ("----------- Thread Outque:% D Is Working! / n ", para-> id); // Para-> BQ-> Outqueue (Para-> BUF, Para-> LEN); // Para-> BQ-> Showbytesqueue (* (PARA-> BQ)); PARA- > BS-> POP (Para-> BUF, Para-> LEN); Para-> BS-> Showbytesstack (* (Para-> BS)); Usleep (Para-> Delay); i ;} pthread_exit (null }

INT main () {// ipstack :: ipstack (int size) // size = 10 uint_8 mybuf1 [] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x50, 0x58, 0x0d, 0x0d, 0x0d, 0x08, 0x00, 0x45, 0x00, 0x00, 0x34, 0x00, 0x40, 0x11, 0x00, 0x40, 0x11, 0xa8, 0x21, 0x0f, 0xc0, 0xa8, 0x21, 0x02, 0x04, 0x01, 0x00, 0x05, 0x00, 0x20, 0x60, 0x4c, 0x73, 0x66, 0x61, 0x73, 0x64, 0x66, 0x73, 0x61, 0x64, 0x66, 0x61, 0x73, 0x64, 0x66, 0x73, 0x64, 0x61, 0x66, 0x61, 0x73, 0x66, 0x73, 0x64, 0x66}; uint_8 mybuf2 [100]; int Len; Bytesstack BS (100); MyParameter Paras [4] = {{0, & BS, MyBuf1, 66, 1000000}, {1, & BS, Mybuf1, 66, 2000000}, {2, & BS, Mybuf1, 66, 300000}, {3, & BS, Mybuf2, Len, 1000000}}; //bq.showbytesqueue (BQ); pthread_attr_init (& Attr); pthread_create (& Threads [& threads [& threads [& threads [ , & Attr, Push, (Void *) & Paras [0]); Pthread_create (& Threads [1], & Attr, Push, (Void *) & Paras [1]); Pthread_create (& Threads [2], & Attr, Push, (void * ) & paras [2]); pthread_create & Threads [3], & Attr, POP, (Void *) & Paras [3]); // pthread_create (& Threads [4], & Attr, POP, (Void *) & Paras [3]); for (int i = 0; i <4; i ) {pthread_join (threads [i], null;} pthread_attr_destroy (& attr);

转载请注明原文地址:https://www.9cbs.com/read-82358.html

New Post(0)