Thread: Thread safety for tiny simple functions

  1. #16
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    In an attempt to get quicker synchronization, I tried to implement a naive barrier system, using a busy wait, thinking that maybe not invoking system threading code would speed things up:
    #include <utility>
    #include <boost/thread.hpp>
    #include <cmath>
    #include <iostream>
    const unsigned int lesize = 100000;
    const unsigned int letime = 100000;
    double v[lesize];
    double u[lesize];
    struct task
       enum state { running = 1, waiting = 0 };
       task(state & my_state, state & other_state, std::pair<unsigned int,unsigned int> interval) : me(my_state), it(other_state), r(interval) {}
       void operator()()
    	for(int t = 0; t < letime; ++t)
    	   me = running;
    	   for(int i = r.first; i < r.second; ++i)
    		u[i] = v[i-1] + v[i+1] - 2.0*v[i];
    	   me = waiting;
    	   while(it == running) /*nothing*/;
    	   me = running;
    	   for(int i = r.first; i < r.second; ++i)
    		v[i] += 0.001 * u[i];
    	   me = waiting;
    	   while(it == running) /*nothing*/;
       state & me;
       state & it;
       std::pair<unsigned int,unsigned int> r;
    int main()
       for(int i = 0; i < lesize; ++i)
    	v[i] = std::sqrt( ((lesize+3)/2.0)*((lesize+3)/2.0) - (i-lesize/2.0)*(i-lesize/2.0) ); //semicircular arc, as initial condition
       boost::thread_group tg;
       task::state state1, state2;
       state1 = state2 = task::running;
       tg.create_thread( task(state1,state2, std::make_pair(1,lesize/2)) );
       tg.create_thread( task(state2,state1, std::make_pair(lesize/2, lesize-1)) );
       std::cout << "Complete." << std::endl;
    It hangs indefinitely. But why? Does constant checking block setting?
    I posted a whole program so that you can run it without having to spend time.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #17
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    >> But why?
    You're not synchronizing access to state1 and state2. In general, when two threads access (read/write) the same memory location, synchronization is required. You have to use the synchronization primitives provided by the threading library.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help on a simple functions program
    By blankstare77 in forum C++ Programming
    Replies: 3
    Last Post: 08-14-2005, 07:06 AM
  2. How to make a thread sleep or std::recv timeout?
    By BrianK in forum Linux Programming
    Replies: 3
    Last Post: 02-26-2003, 09:27 PM
  3. MFC Controls and Thread Safety :: MFC
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 12-06-2002, 10:36 AM
  4. Member Functions as Thread Procs
    By johnnie2 in forum C++ Programming
    Replies: 3
    Last Post: 12-01-2002, 06:14 PM
  5. Need help with simple overloaded functions and pointers
    By smokedragon in forum C++ Programming
    Replies: 2
    Last Post: 03-20-2002, 08:15 PM