Do I need to synchronize?

This is a discussion on Do I need to synchronize? within the Windows Programming forums, part of the Platform Specific Boards category; Just wondering about this, if I want to do something like this in a thread, do I have to synchronize ...

  1. #1
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879

    Question Do I need to synchronize?

    Just wondering about this, if I want to do something like this in a thread, do I have to synchronize them?
    Code:
    (example 1)
    //global vector
    std::vector<Object> objects;
    
    //main code
    objects.back().doSomething();
    
    //thread code
    objects.pop_back();
    
    (example 2)
    //global vector
    std::vector<Object> objects;
    
    //main code
    objects[0].move();
    
    //thread code
    objects[0].draw();
    In example 1, is it possible for the object to get deleted before doSomething() completes?
    And in example 2, is it possible that something will go screwy with the stack pointer or something (I don't really know what I'm talking about, and my books don't help) or is Windows smart enough to be sure everything happens properly?

    In both examples, will I need to use a critical section or something related to ensure exclusive access to the vector?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Yes, you have to syncronize them. Yes, you can use a critical section. If there is only one shared resource you can use a simple lock:

    Code:
    struct sentry {
    sentry(bool & key);
    ~sentry();
    void lock();
    void unlock();
    bool & _locked;
    bool mine;
    };
    sentry(bool & key, bool now = false)
     :_locked(key)
    {
     mine = false;
     if(now)
      lock();
    }
    ~sentry()
    {
     unlock();
    }
    void lock()
    {
       if(!mine)
      {
        while(_locked)
         continue;
        mine = _locked = true;
      }
    }
    void unlock()
    {
      if(mine)
     {
      mine = _locked = false;
     }
    }
    
    
    
    
    
    //global vector
    std::vector<Object> objects;
    bool key = false;
    
    //main code
    sentry gate(key);
    gate.lock();
    objects.back().doSomething();
    gate.unlock();
    
    
    //thread code
    sentry gate(key, true);
    objects.pop_back();
    gate.unlock();
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    So I need to synchronize them in example 2 too, right?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Of course you do. Any data structure that will be accessed from different threads must *always* be controlled by syncronization objects. Consider a simple array of chars. If one thread is copying the array while the other is reading it - what do you think will happen? Corruption. Pure and simple.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>>
    Any data structure that will be accessed from different threads must *always* be controlled by syncronization objects.
    <<<

    Not necessarily. If all threads are only reading common data, no synch is necessary at all.

    If a collection of threads at start increment a shared thread counter, then decrement it as they exit, again, there is no synch needed.

    The are other examples. Generally you will want to synch shared data, however, synching when it is not necessary simply hacks at the programs performance.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do you synchronize them?
    By audinue in forum Tech Board
    Replies: 1
    Last Post: 01-01-2009, 12:49 PM
  2. Synchronize the computertime
    By franse in forum C++ Programming
    Replies: 1
    Last Post: 10-26-2008, 06:44 AM
  3. SYnchronize read & write of a cache file
    By lei_michelle in forum C Programming
    Replies: 4
    Last Post: 02-26-2008, 05:49 PM
  4. Replies: 0
    Last Post: 10-06-2007, 02:25 PM

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