Thinking of two multi-threaded written test questions

Source: Internet
Author: User

A point of view of their own, there is a wrong place please advise

1, A,b,c is a global variable, two threads execute the following code in parallel, then the final result of a may be


Four statement permutations (the statement order of each program cannot be reversed)

The result is 4,13,15,26


Note: There is no writeback problem, because a,b,c are global variables, two threads share these variables, and there are no self-operating operators in the program, so there is no writeback problem


2.

Answer a

Note: There is a writeback problem here, because there are self-operating operators, that is, ++a, which the compiler should handle
temp=a+1;
A=temp;
And A=temp This is the write back operation, temp is a temporary variable, exists in each thread's stack space (that is, two threads are different temp), and this sentence and printf execution order is indeterminate


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.