Http://news.ddaaoo.com/Topic/view/id-57255
Message Queue for Linux:
Mq_open is used in logging. When a new log is added to the queue, I send a message to mq_send. The logging thread starts to record the data until the log queue is empty and then waits.
However, we encountered the first situation: a long wait like a deadlock! Because if it is set to blocked, mq_send will return after the message is successfully sent! This can easily lead to deadlocks! Imagine if a large number of messages are filled in the queue, and the queue is always full, there will be many mq_send messages that have to wait! Therefore, we recommend that you add o_nonblock to the mq_open parameter! However, this will also cause the ealign error, which is a non-blocking error.
Next is the second case. Mq_receive failed! If the size of the received buffer is different from that set during mq_open, this situation occurs.
Therefore, I first set it to blocking. In addition, for me, I only need to know that there is a message, so I do not need to receive each message accurately, as long as there is a message in the message queue, then I will be notified by the system.
Therefore, my settings are as follows:
1. Create a Message Queue
2. Custom message sending:
# Define time_non_100 100
Mqd_t g_my_mq_send (mqd_t mqdes const char * msg_ptr
Size_t msg_len unsigned msg_prio );
Mqd_t g_my_mq_send (mqd_t mqdes const char * msg_ptr
Size_t msg_len unsigned msg_prio)
{
Struct timespec T;
T. TV _sec = 0;
T. TV _nsec = time_non_100;
Return mq_timedsend (mqdes msg_ptr msg_len msg_prio & T );
}
Here, I set the timeout time to 100 nanoseconds. If we fail to send a victory message within 100 nanoseconds, we will return it directly!
3. General reception
4. Disable Message Queue
Tested, performance increased by N times!
However, I think this is not enough! 100 s is also time. For humans, 100 s is insignificant, but it is different for computers. So,CodeChanged:
Mqd_t g_my_mq_send (mqd_t mqdes const char * msg_ptr
Size_t msg_len unsigned msg_prio)
{
Struct timespec T;
T. TV _sec = 0;
T. TV _nsec = 1;
Return mq_timedsend (mqdes msg_ptr msg_len msg_prio & T );
}
One second! A little faster!
Still not good! CPU switching and system clock interruption may affect performance! So, as follows:
Mqd_t g_my_mq_send (mqd_t mqdes const char * msg_ptr
Size_t msg_len unsigned msg_prio)
{
Struct timespec T;
T. TV _sec = 0;
T. TV _nsec = 0;
Return mq_timedsend (mqdes msg_ptr msg_len msg_prio & T );
}
The improvement is not obvious, but it is basically impossible. Theoretically, it should be faster.
It seems that it is still not very good. Change it again:
Inline mqd_t g_my_mq_send (mqd_t mqdes const char * msg_ptr
Size_t msg_len unsigned msg_prio)
{
Struct timespec T;
T. TV _sec = 0;
T. TV _nsec = 0;
Return mq_timedsend (mqdes msg_ptr msg_len msg_prio & T );
}
So inline, OK!
By: zhanyonhu