Synchronization Question

This is a discussion on Synchronization Question within the C Programming forums, part of the General Programming Boards category; I've been trying to figure out when and where a context switch can occur. I thought I understood it but ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    3

    Synchronization Question

    I've been trying to figure out when and where a context switch can occur. I thought I understood it but a program I wrote seems to be proving me wrong.

    Here's the setup. I've declared a global unsigned int val and in my main method I give it the value 2097152. I also created a global volatile char called hold which I've set to true in the main method. I then create 10 threads and have them run the following function:

    Code:
    void * thread_work(void * parameters)
    {
       while(hold) {}
       val = val / 2;
       return NULL;
    }
    As soon as all the threads are created main will set hold to some value that evaluates to false. (I did this because I didn't want any threads executing while I'm still creating the others.)

    Here's my question, why am I consistently getting the result 2048? Shouldn't I be getting random power of 2 values between that and 1048576? I thought the code might execute something like this:

    thread1 moves memory value into register
    thread1 divides value (lets say it's the first one so 1048576)
    *context switch*
    thread2 moves memory value into register (still 2097152)
    thread2 divides value (lets say it's the first one so 1048576)
    thread2 moves register value back into memory
    *context switch*
    thread1 moves register value back into memory

    Which would make the result 4096 at least... so why isn't this happening? What am I misunderstanding here?

    I posted this on another forum and someone mentioned that it could be since the work is so quick the threads aren't switching which makes some sense. However I'm running this on a dual core system so two of the threads are running simultaneously which, I would think, would result in the erratic behavior I was expecting

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,660
    >> I've been trying to figure out when and where a context switch can occur. I thought I understood it...
    In general, a context switch can happen at any time and any place. This should be your mentality when designing and reviewing multi-threaded code.

    The code you've posted invokes "undefined behavior" - which speaks for itself. So really defining the behavior requires knowledge of your specific hardware running the code, as well as an analysis of the assembly code generated by your specific compiler.

    You're witnessing undefined behavior in all its glory. I'd concentrate on what makes a well-defined multi-threaded application.

    gg

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    thread1 divides value (lets say it's the first one so 1048576)
    *context switch*
    thread2 moves memory value into register (still 2097152)

    Whilst this scenario is indeed true, context switches are many thousands of instructions apart. With such short code, the show is over before anything interesting has had chance to occur.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    3
    Quote Originally Posted by Salem View Post
    thread1 divides value (lets say it's the first one so 1048576)
    *context switch*
    thread2 moves memory value into register (still 2097152)

    Whilst this scenario is indeed true, context switches are many thousands of instructions apart. With such short code, the show is over before anything interesting has had chance to occur.
    Granted but I'm running this on a dual core system like I said so that should produce the erratic results I'm expecting too... shouldn't it?

    I guess my main problem is I don't feel like I have any way to figure this stuff out for myself. Any application I write is simply going to be a second hand look at what is really going on. Are there any tools (linux/windows) or resources that would give me some more reliable information?

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Except it takes work to schedule a thread as well. While one core is running (and finishing) a thread, the other core is setting up the next thread to run (and then run it).


    If each thread did this, then you may see anomalies in a few entries in the array.
    int array[100000000]; // initialise all to some value

    Code:
    for ( i = 0 ; i < 100000000 ; i++ ) {
      array[i] /= 2;
    }

    > Are there any tools (linux/windows) or resources that would give me some more reliable information?
    Any intrusive technique will change the effect you see, and thus they usually end up masking the very problem you're trying to solve.

    Really good design telling you what is shared (and therefore needs guarding in some way), and what is private is essential for thread work.

    http://c2.com/cgi/wiki?ThreadsConsideredHarmful
    http://www.kuro5hin.org/story/2002/11/18/22112/860
    If you really want to make use of multiple cores (when you have them), then there isn't a lot of choice but to use threads.

    Unless you're at the stage where you're totally comfortable debugging any linear (single thread) program, then multiple threads will just screw with your mind.

    Perhaps Helgrind, but read the disclaimers.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using this as synchronization object
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 03-22-2008, 07:49 AM
  2. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  3. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21