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
reference 3 floor qybao 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 is judging process of the queue of data processing, otherwise empty polling,

CodePudding user response:

Have no lock can lock, as long as the multi-thread safe access at the same time, under the

You said lock STD: : queue Also has a lot of way, if you have no known bugs of a way to achieve, can you send a link? Which is most suitable for me in comparison, thank you,

reference 1st floor SDGHCHJ response:
multithreaded you unlocked?

Is it not a lock of STD: : queue It is enough?

CodePudding user response:

Can, hope verified no bug, is there a link, I see,

refer to the second floor enic response:
to be thread-safe lock-free queue?

CodePudding user response:

refer to the eighth floor registercsdn response:
have no lock can lock, as long as the multi-thread safe access at the same time, under the

You said lock STD: : queue Also has a lot of way, if you have no known bugs of a way to achieve, can you send a link? Which is most suitable for me in comparison, thank you,

Quote: refer to 1st floor SDGHCHJ response:

Multithreaded you unlocked?

Is it not a lock of STD: : queue It is enough?
that this is where it is safe to visit you? The queue empty safe?

CodePudding user response:

STL or Qt containers are not thread-safe, across threads based on these items for operation, all need large-grained mutex protection, such as to protect the whole operation, because small granularity operation such as traversal, it is important for the maintenance of the queue status is,

Write:
 
The lock (); a mutex.
//..
For (... )
QueueMutex. Unlock ();


Read:
 
Queue contents//copy need to be addressed and released as soon as possible,
//there are multiple threads at the same time for the operation,
The lock (); a mutex.
The Queue TMP=Queue. Pop_first (100);
Mutex. Unlock ();

The foreach item in TMP...
Deal (item);//time-consuming operation,



If you are in a small granularity parallel, it is recommended to use the database as a queue, because in the process of traversing the deletion of the data set increases, involve the atomicity, integrity protection, version and so on the characteristics of complex,

CodePudding user response:

Yes, I am just above the pseudo code, probably describe demand, is not a security access,
I was to write a class, similar SafeQueue, add the security access in a SafeQueue, use the SafeQueue directly when the Queue is used, SafeQueue internal processing locks and security,

references to the tenth floor SDGHCHJ response:
Quote: refer to the eighth floor registercsdn response:
have no lock can lock, as long as the multi-thread safe access at the same time, under the

You said lock STD: : queue Also has a lot of way, if you have no known bugs of a way to achieve, can you send a link? Which is most suitable for me in comparison, thank you,

Quote: refer to 1st floor SDGHCHJ response:

Multithreaded you unlocked?

Is it not a lock of STD: : queue It is enough?
that this is where it is safe to visit you? The queue empty safe?

CodePudding user response:

On the third floor right, if only two threads, a read a write, do not need to lock,

CodePudding user response:

nullnull
  • Related