Thread: boost::thread return value

  1. #1
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463

    boost::thread return value

    I'm using boost::thread to add number from 0 to 1,000,000,000. I split the thread into adding odd numbers and even numbers.
    Code:
    #include <boost/date_time/posix_time/posix_time.hpp>
    #include <boost/cstdint.hpp>
    #include <boost/thread.hpp>
    #include <iostream>
    
    class Test
    {
    private:
            int even_odd;
            boost::uint64_t sum ;
    
    public:
        Test(int what , int s = 0): even_odd(what) , sum(s) {};
    
        void operator() ()
        {
            for (int i = even_odd; i < 100000; i += 2)
                sum += i;
            std::cout << sum << std::endl;
        }
    
        boost::uint64_t operator+(const Test & Second) const
        {
            std::cout << sum << std::endl;
            std::cout << Second.sum << std::endl;
            return sum + Second.sum;
        }
    
        void print()
        {
            std::cout << sum << std::endl;
        }
    };
    
    int main()
    {
    
        boost::posix_time::ptime start = boost::posix_time::microsec_clock::local_time(); // start calculating time
    
        Test even(0); // add even numbers;
        Test odd(1);  // add odd numbers
        boost::thread t1(even); //start adding even
        boost::thread t2(odd);  //start adding odd
        t1.join(); //put main to sleep
        t2.join(); //put main to sleep
        boost::uint64_t sum = even + odd;
    
        boost::posix_time::ptime end = boost::posix_time::microsec_clock::local_time();
        std::cout << end - start << std::endl;
    
        std::cout << "The sum of number from 0 to 1000000000 is " << sum << std::endl;
    
        return 0;
    }
    The sum is keep coming up to be zero. I realize that because boost::thread() creates a copy of the class instead. Is there away to obtain return value from a thread?
    "All that we see or seem
    Is but a dream within a dream." - Poe

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Code:
        boost::thread t1(boost::ref(even)); //start adding even
        boost::thread t2(boost::ref(odd));  //start adding odd
    gg

  3. #3
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    gotcha. it works now. thank you. what happens if i pass in a function instead of a class? Would boost::lambda work as a way to obtain the return of the function?
    "All that we see or seem
    Is but a dream within a dream." - Poe

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Class Test provides the 'return value' via its 'sum' member. You have to provide the access for any boost::thread return value.

    There are also "futures" that are analogous to multi-threaded "return values" - Synchronization - Boost 1.46.1

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Boost thread
    By kargo in forum C++ Programming
    Replies: 2
    Last Post: 04-06-2011, 02:26 AM
  2. WaitForMultipleObjects for Boost.Thread?
    By Elysia in forum C++ Programming
    Replies: 8
    Last Post: 05-23-2009, 12:18 PM
  3. Terminate Boost::Thread
    By CornyKorn21 in forum C++ Programming
    Replies: 3
    Last Post: 06-03-2008, 12:19 PM
  4. boost::thread
    By l2u in forum C++ Programming
    Replies: 4
    Last Post: 11-08-2006, 03:59 PM
  5. Boost thread not working
    By Monkeymagic in forum C++ Programming
    Replies: 4
    Last Post: 08-20-2006, 01:50 AM