Home > Back-end > One can read and write at the same time the Queue (Queue), the best lightweight, have been tested th
One can read and write at the same time the Queue (Queue), the best lightweight, have been tested th
Time:09-24
There are many online, but there is no guarantee that was verified, no bug, ask for you advice, recommend a bug, widely used by developers, lightweight, best
Below is the usage and requirements, 1. 10 I assigned A buffer, A user in A thread to write, user B in another thread to read, and to ensure the order, when B read must, in accordance with A written order,
2. My design is that there is a queue, queue is to ask for, I set up three queue, queue_empty, queue_write, queue_read.
3. 1). The first step: initialization, the index of 10 buffer: 0, 1, 2,... 9 to join queue_empty, indicating that the 10 buffer is empty now, for A user to write,
for (int I=0; I<10; I++) { Queue_empty. Push (I); }
2). The second step: this step will loop operation in one thread, A user to write content, from queue_empty take out an index first, and then write the content, put the index to join queue_write that buffer [index] have content
if (queue_empty!=empty) { The index=queue_empty. Pop ();
The buffer [index] to write;
Queue_write. Push (index); }
3). The third step: this step will also loop in a thread running, B users to read the content, from queue_write take out an index first, and then read the content and the analysis of the content, after use the index to join queue_empty, explain buffer [index] content has been read, now continue on for the use of the second step of write operation in queue_empty
if (queue_write!=empty) { The index=queue_write. Pop ();
To buffer [index] read operation and analysis;
Queue_empty. Push (index); }
now please help to provide such a class or structure of the Queue, I can use them directly, directly defined as Queue q_empty, Queue q_write, Queue q_read. Or you have better than the above permission while maintaining the order of the read/write mechanism, also can bring up,
CodePudding user response:
Multithreaded you unlocked?
Is it not a lock of STD: : queue It is enough?
CodePudding user response:
To be thread safe lock-free queue?
CodePudding user response:
That you write a good The class Que { Private: STD: : queueSTD: : queueSTD: : queuePublic: . } If there are only two threads (a read write), and don't lock can also, each thread to judge his handling of the queue of data processing, otherwise empty polling,
CodePudding user response:
Iocp message queue,
CodePudding user response:
Write their own oh, writing is not have the ability to write their own, not particularly likely, don't be afraid to write a bug, have the ability to change is ok
CodePudding user response:
Ring can be achieved without the lock
CodePudding user response:
One read, write, and also need the lock, at the same time, speaking, reading and writing, is likely to undermine their queue the internal counter, The