We know that volatile is associated with the compiler optimization, for multi-threaded Shared global variables, we usually declared volatile variables, so I made the following experiment, choose the GCC 5.4.0 compiler, found in - O0 - - optimal levels of O3, output are consistent with the results, the program did not see the consequences of not declared volatile
#include #include #include Void * child_pth_fun (void * arg); //Shared global variables Int counter=1; Int main () { Int a=counter; Pthread_t child_pth_id; Pthread_create (& amp; Child_pth_id, NULL, child_pth_fun, NULL); Sleep (1); Int b=counter; Printf (" a=% d, b=% d \ n ", a, b); Pthread_join (child_pth_id, NULL); return 0; } Void * child_pth_fun (void * arg) { Printf ("====enter child pthread====\ n "); Counter=100; Printf (" counter had changed \ n "); Printf ("====exit child pthread \ n "); } Reference
compile command: GCC - O3 - o pthread pthread. C - lpthread The reference program output: ====enter child pthread==== Counter had changed ====exit child pthread A=1, b=100 Reference there is a saying, each thread has its own stack, so Shared global variables will appear not synchronous thread safety problem, lead to two threads operation is not the same variable http://blog.csdn.net/weixin_39400271/article/details/86499978 CodePudding user response:
His help,,,, CodePudding user response:
Who tell you if use volatile in multithreaded, I didn't use volatile in multi-threaded, and global variables, only one, I don't know what is the reference, do you think, I didn't see, but the thread has its own stack and global variables have what relation? If you want to solve the problem of thread safety, you can use the lock, use volatile can solve the problem of what, in general, in addition to direct operation registers or memory program, rarely use volatile; Volatile are commonly used to prevent being optimized, each time directly from the corresponding memory or register values, and thread safety what relation??? CodePudding user response:
Volatile ensures that each visit this variable, is in the memory address access, will not be compiler optimization into register variables, if the volatile variables into registers, without limit, he may access the variable from memory access to register for the first time, to the efficiency of this register is not occupied, next time you visit can directly read the value of the register, if change variables during the memory address (multithreaded), did not change the value in the register, use it for this time synchronization problems or symbol variables, CodePudding user response:
reference gouyanfen reply: 3/f volatile ensures that each visit this variable, is in the memory address access, will not be compiler optimization into register variables, if the volatile variables into registers, without limit, he may access the variable from memory access to register for the first time, to the efficiency of this register is not occupied, next time you visit can directly read the value of the register, if change variables during the memory address (multithreaded), did not change the value in the register, use it for this time synchronization problems or symbol variables, Building positive solution, I have not found a volatile above problems, is due to the sleep (), the compiler should have some consumption such as sleep a long time operation has been optimized, CodePudding user response:
reference not smell out the window on the second floor response: who tell you if use volatile in multithreaded, I didn't use volatile in multi-threaded, and global variables, only one, I don't know what is the reference, do you think, I didn't see, but the thread has its own stack and global variables have what relation? If you want to solve the problem of thread safety, you can use the lock, use volatile can solve the problem of what, in general, in addition to direct operation registers or memory program, rarely use volatile; Volatile are commonly used to prevent being optimized, each time directly from the corresponding memory or register values, and thread safety what relation??? Thread lock is to solve the problem, and the application process related to volatile solve the optimization problem,, and the compiler, CodePudding user response:
Solved, find a way to test, found the compiler to do a lot of optimization, the necessity of doing experiments to understand volatile in multithreading and that kind of trouble, Use the code: #include #include #include #include Int a=1; Void * child_pth_fun (void * arg); Int main () { Int b, c; Volatile int val=10000000; //create the child thread Pthread_t child_pth_id; Pthread_create (& amp; Child_pth_id, NULL, child_pth_fun, NULL); B=a; //simulation a long paragraph, consumption is the main thread time While (val -); C=a; If (c==b) Printf (" In the main pthread: a=% d, b=% d, c=% d \ n ", a, b, a); Pthread_join (child_pth_id, NULL); return 0; } Void * child_pth_fun (void * arg) { //the child thread modify Shared global variables A=4; Printf (" the child In the pthread: a=% d \ n ", a); } Reference compile command: arm - Linux - GCC - O3 -o pthread pthread. C The reference output: In the child pthread: a=4 In the main pthread: a=1, b=1, c=1 CodePudding user response:
I hope it can help you: https://blog.csdn.net/it_xiangqiang/category_10581430.html I hope it can help you: https://blog.csdn.net/it_xiangqiang/category_10768339.html