Thinking of two multi-threaded written test questions

Source: Internet
Author: User

A point of view of their own, there is not the right place please advise

1, A,b,c is a global variable, two threads run the following code in parallel, then A's finally result may be


Four statement permutations (each program's statement order cannot be reversed)

The result is 4,13,15,26


Note: There is no writeback problem, since a,b,c are global variables, two threads share these variables. And there is no self-operating operator in the program, so there is no writeback problem


2.

Answer a

Note: There is a write back problem here. Because of the existence of the self-operation operator. That is ++a, the compiler should handle this
temp=a+1;
A=temp;
And a=temp This sentence is write back operation. Temp is a temporary variable that exists in the stack space of each thread (that is, two threads are different temp). And this sentence is not deterministic with the order in which printf is run


Thinking of two multi-threaded written test questions

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: info-contact@alibabacloud.com 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.