In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Today, I will talk to you about how the learning process of RT-Thread inter-thread communication is. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
Here's a summary of the last important point of knowledge related to RT-Thread multithreading: communication between threads. The previous article mentioned many times that a large task is divided into multiple small tasks, and there must be a variety of relationships between these small tasks, so that the threads of these small tasks cannot go their own way. the operation of other task threads must be considered.
Now that you have synchronization between threads that allows multiple threads to communicate with each other, why do you need to communicate between threads? What exactly is inter-thread communication, and what are the application scenarios for this approach? The editor tries to summarize the learning process of communication between RT-Thread threads from the following aspects.
Concepts related to inter-thread communication
What is inter-thread communication? Communication, as the name implies, is the need for communication and dialogue between the two parties. In a popular way, thread A has some data or information during its running period, which should be told to thread B so that thread B can continue to complete the specified task and work after receiving the data or information.
Why do two threads communicate with each other? Again, multiple task threads are not independent. When they are working, they need to communicate according to the business scenario, or take the music player as an example. When the lyrics reading thread reads the lyrics from the hard disk, tell the display thread this string of lyrics to display the lyrics. This "tell" action is carried out through communication between threads.
2.png
Since it's all about coordinating the working state of threads, what's the difference between synchronization between threads and communication between threads? The difference is that what synchronization between threads can do is too limited. Synchronization between threads just tells each other, "Don't run too fast, wait for me." while communication between threads, there is a lot of data and information to tell each other. In case A thread has a lot to say to B thread, thread synchronization can not meet the requirements, so inter-thread communication is needed.
During the use of a mailbox, there may be an empty mailbox or a full mailbox. When the mailbox is empty, the thread that receives the mail will choose to suspend waiting, or wait for the timeout to arrive. When the mailbox is full, the sending thread chooses to suspend or directly return a mailbox full return value.
The system kernel provides the following mailbox-related API function interfaces, as shown in the following figure.
When thread A wants to send a message, first take a block space from the free message block list, load the message into it, and connect the message block to the end of the line of the non-empty message block list. If the message is sent in an emergency way, the message block is hung to the head of the queue of the non-empty message list. When a thread gets a message, it always gets the message in the chain header.
The system kernel provides the following message queue-related API function interfaces, as shown in the following figure.
The system kernel provides the following signal-related API function interfaces, as shown in the following figure.
Matters needing attention for communication between threads
When communicating between multiple threads, there are some considerations about mailboxes, message queues and signals.
1. When using mailboxes for inter-thread communication, because a message can only be 4 bytes long at most, if you want to transmit more data information, you can use structures to encapsulate information and pass it through pointers.
two。 Mail delivery is non-blocking, so it can be applied to interrupting service programs. But mail reception is blocked, you can set the receive timeout time, can not be used in the interrupt service program to receive mail.
3. When there is no mail in the mailbox and the timeout is not 0, the receiving process of the message automatically becomes blocked. When the mailbox is full, the sending thread can choose to suspend waiting or directly return the error code of the mailbox full.
4. Message queuing is an asynchronous mode of communication. Messages in message queues always follow the principle of first-in, first-out.
5. You can send messages to the message queue in a thread or interrupt service program, but you cannot receive messages in the interrupt service program.
6. An emergency message can be sent to the message queue, and the emergency message will be placed in the chain header of the message queue and will be first obtained by the waiting thread.
7. Signal is different from semaphore, so the concepts of signal and semaphore should not be confused. Signal is a kind of soft interrupt mode on the software level.
8. The thread does not wait for the signal to arrive in a blocking way, because the thread itself does not know when the signal (soft interrupt) will arrive.
9. The processing of the signal by the thread can be set to capture the signal, ignore the signal, and process the signal in the default way.
After reading the above, do you have any further understanding of the learning process of RT-Thread inter-thread communication? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.