Find the answer to your Linux question:
Results 1 to 5 of 5
Setting a global flag for multiple child threads. In all child thread, the global variable will only be read. For sure the read can happen simultaneously. Totally Linux user space ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined! legendbb's Avatar
    Join Date
    Mar 2007
    Location
    Canada
    Posts
    27

    Cool Global read only variable needs mutex?


    Setting a global flag for multiple child threads.

    In all child thread, the global variable will only be read. For sure the read can happen simultaneously. Totally Linux user space access, do I need to use mutex to protect the read?

    Thanks,

  2. #2
    Just Joined!
    Join Date
    Dec 2009
    Location
    California
    Posts
    98
    No, you do not need a mutex if the value will only be read.

  3. #3
    Just Joined! legendbb's Avatar
    Join Date
    Mar 2007
    Location
    Canada
    Posts
    27

    resolved

    Great thanks,

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,748
    Actually, you should lock the variable when the main thread is modifying it, or else use an update statement that is atomic in operation so that the variable is assured to NOT be updated at the same time a child thread is reading it, otherwise your child thread(s) may get garbage. Atomic updates can be done to normal scalar operations where the variable is of a type that is no bigger than the machine word size (32 or 64 bits typically). This means that single chars, ints, etc can be so updated. These subjects have been discussed often on the web in such publications as Dr. Dobbs, Embedded System Design, Software Magazine, etc. A google search on the terms "atomic updates c" will give you a list of some useful links.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  6. #5
    Just Joined!
    Join Date
    Dec 2009
    Location
    California
    Posts
    98
    Yeah, I guess I missed the inference that the main thread would be modifying the variable. Thanks for pointing that out Rubberman.

    I still don't think it needs to be locked in this situation, however, the variable must be declared as "volatile" or todays compilers will optimize away the loading of the register.

    For example, if you have something like this in a child thread:

    int finished=0;
    while (!finished) {
    printf("hello\n");
    }

    And you set "finished=1" in a different thread, it's likely that this thread will never know about it unless you declare finished as:
    volatile int finished=0;

    Actually, you could try this. Compile it without the -O and run it, then compile it with -O3 and run it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •