Multi-threaded thing (data synchronization)

Source: Internet
Author: User

"Disclaimer: Copyright, welcome reprint, please do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "

Multithreaded creation is very simple, there are many functions under the Windows system can create multi-threading, such as _beginthread. We can use it to write a simple multithreaded code for us,

[CPP]View Plaincopy
  1. #include <windows.h>
  2. #include <process.h>
  3. #include <stdio.h>
  4. unsigned int value = 0;
  5. void print (void* argv)
  6. {
  7. While (1) {
  8. printf ("&value =%x, value =%d\n", &value, value);
  9. Value + +;
  10. Sleep (1000);
  11. }
  12. }
  13. int main ()
  14. {
  15. _beginthread (print, 0, NULL);
  16. _beginthread (print, 0, NULL);
  17. While (1)
  18. Sleep (0);
  19. return 1;
  20. }

Note that you need to turn on the/MD switch when compiling the VC. For the specific operation, "project", "setting", "C + +"->category "Code Generation", "Use Run-time library", "Debug Multithreaded "can.

From the example above, we see that value as a shared variable can actually be accessed by all threads. This is the biggest advantage of thread data synchronization--convenience, direct. Because there are different stack spaces between threads, both the code snippet and the data segment are in one space. So, threads want to access public data, they can access public data without any restrictions.

Of course, everything has its two sides. This access pattern to public resources can also cause some problems. What's the problem? We'll find out when we see it.

Now suppose there is a pond where we hire two people to feed the fish. Two of people kept feeding the fish in the pond. We stipulate that when one person feeds the fish, another person does not need to feed the fish, or the fish will feed two times to die. To do this, we have installed a brand as a warning. If a person is feeding the fish, he will set the sign to false, then another person will see this brand, will not continue to feed the fish. By the time the man had finished feeding, he continued to set the sign to true.

If we need to write this story in code, then how? Friends try,

[CPP]View Plaincopy
    1. while (1) {
    2. if (flag = = true) {
    3. Flag = false;
    4. Do_give_fish_food ();
    5. Flag = true;
    6. }
    7. Sleep (0);
    8. }

The code above looks fine, but look at the code assembly code to see if there is a hidden problem. Because there will be two people feeding the same situation,

[CPP]View Plaincopy
  1. : While (1) {
  2. 004010E8 mov eax,1
  3. 004010ED Test Eax,eax
  4. 004010EF JE do_action+56h (00401126)
  5. : if (flag = = true) {
  6. 004010F1 cmp DWORD PTR [flag (00433E04)],1
  7. 004010f8 jne do_action+43h (00401113)
  8. 25:flag = false;
  9. 004010FA mov dword ptr [flag (00433E04)],0
  10. 26:do_give_fish_food ();
  11. 00401104 call @ILT +15 (Do_give_fish_food) (00401014)
  12. 27:flag = true;
  13. 00401109 mov dword ptr [flag (00433E04)],1
  14. 28:}
  15. 29:
  16. 30:sleep (0);
  17. 00401113 mov Esi,esp
  18. 00401115 Push 0
  19. 00401117 call DWORD ptr [[email protected] (004361C4)]
  20. 0040111D CMP ESI,ESP
  21. 0040111F call __chkesp (004011E0)
  22. 31:}
  23. 00401124 jmp do_action+18h (004010e8)
  24. 32:}

We now assume that there are two threads A and b that are constantly judging and feeding operations. Set Current flag = True, when thread a executes to 004010f8, it is determined that the fish has not been fed, is preparing to execute instruction 004010f8, but there is no time to set the Falg, there is a thread dispatch. When thread B runs to 004010f8, it is found that no one is currently feeding, so a feeding operation is performed. Wait until the B thread is finished feeding, and when it runs to 00401113, the dispatch occurs again. Thread A continues to run because it has previously been judged that no feed has been fed, so thread a continues to feed the operation. So, poor fish, this time on a continuous two feeding operation, it is estimated that a part of the fish to die.

Multi-threaded thing (data synchronization)

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.