Thread operations share a little share of variables.

Source: Internet
Author: User
Tags message queue volatile

Some time ago in order to solve the VoLTE's T-side short Message flow problem, I wrote a T-side short message tool, on the base thread to engage 3 threads. Because the company's executive machine is more than 4 cores of virtual machines, so the number of threads is completely fine.

The thread created at the start of the program is used for maintenance, such as how many messages are received, how many messages are sent, and what kind of messages are there.

Thread 1 is used to receive the message, the message is not dry, the direct turn into a string is thrown into the message queue.

Thread 2 is used to process messages, primarily to differentiate the message type, then generate the message that needs to be returned and throw it into the message queue.

Thread 3 is used to send messages, what happens in the message queue.

There is a shared data interaction between three threads, mainly in the processing of message queues. Message Queuing does not use any special protection mechanism at the time of processing, so the vector can be handled normally. Occasionally, however, there is a case where the program exits unexpectedly. The program architecture is relatively simple and should not have any problems. The handling of billions of news has not seen anything unusual.

So for the program to quit the problem, now suspect that the point is vector thread security.

To this problem, looked at the core programming, think of a kind of manual lock, Simple is to use the volatile variable memory real-time characteristics, manually add a lock. When the lock is applied to determine if it is true, the loop lock is entered as true, and the exit is not true.

A simple lock on the variable is done in this way, and if the data is being processed, wait a minute.

But in this way, it is more worrying if it is not an atomic command, such as a volatile variable set to 1 requires the execution of 3 assembly instructions, a multi-core processor may have a minimum probability of the fight event, are locked.

Thread operations share a little share of variables.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.