Volatile does not guarantee thread safety

Source: Internet
Author: User
Tags volatile

In DPDK, the operation of the atomic weight is encapsulated. Such as
    1. typedefstruct{
    2. volatileint32_t cnt;/**< An internal counter value. */
    3. }rte_atomic32_t;
But a lot of people use it, often as follows
    1. rte_atomic32_t pkt_count;
    2. pkt_count.cnt +=......
This is actually very insecure in multi-threading. Example:
  1. #include<pthread.h>
  2. #include<stdio.h>
  3. volatileint test=0;
  4. //pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
  5. void* print_xs (void* used)
  6. {
  7. //pthread_rwlock_wrlock(&rwlock);
  8. test=test+2;
  9. //pthread_rwlock_unlock(&rwlock);
  10. sleep(1);
  11. //pthread_rwlock_wrlock(&rwlock);
  12. test=test+3;
  13. //pthread_rwlock_unlock(&rwlock);
  14. }
  15. int main (){
  16. pthread_t thread_id[100];
  17. int data=0;
  18. int i=0;
  19. for(i=0; i<100; i++){
  20. pthread_create (&(thread_id[i]), NULL, print_xs, NULL);
  21. }
  22. for(i=0; i<100; i++){
  23. pthread_join(thread_id[i],NULL);
  24. }
  25. printf("%d\n",test);
  26. return-1;
  27. }
The test code, as above, defines thevOlatileand is executed without locking. Execution results are random, not 500. Excerpt from Wikipedia:in theProgramming, especially inC language,C++,C#and theJavalanguage, use the volatile keyword to declare theVariableorObjecttypically have special properties related to optimizations, multithreading. Usually, volatilekeywords are used to prevent (pseudo) compilers from being optimized for code that cannot be "changed by the code itself" (Variables/objects). In the C language, volatileThe keyword can be used to remind the compiler that variables defined later can change at any time, so the compiled program reads the data directly from the variable address each time it needs to store or read the variable. Without the volatile keyword, the compiler might optimize reading and storage, may temporarily use the value in the register, and if the variable was updated by another program, there would be an inconsistency .  



Null



Volatile does not guarantee thread safety

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.