Thread: multithread function collects sum and returns sum

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937

    multithread function collects sum and returns sum

    I'd like to have a function that multiple threads can call, passing an int each. The function will calculate a sum of the ints by using a lock, and then return the sum to each of the threads. I still haven't ironed out my understanding of function argument and return behavior, with multiple threads. Anyway, here's a running of a test program:
    Code:
     $ ./main
    How many threads? 5
    0 in called f.
    1 in called f.
    2 in called f.
    3 in called f.
    4 in called f.
    0
    0
    0
    0
    0
     $
    Code:
    #include <iostream>
    #include <boost/thread/thread.hpp>
    #include <boost/thread/barrier.hpp>
    #include <boost/thread/mutex.hpp>
    #include <vector>
    
    boost::mutex moo;
    int sum;
    
    int f(int in, boost::barrier & bar)
    {
       {
       	boost::mutex::scoped_lock lk(moo);
       	std::cout << in << " in called f." << std::endl;
    	sum += in;
       }
       bar.wait();
       return sum;
    }
    
    struct task
    {
        boost::barrier & bar;
        int total;
        int n;
        task(int n_, boost::barrier & bar_) : n(n_), bar(bar_) {}
        void operator()() { total = f(n,bar); }
    };
    
    int main() 
    {
        std::cout << "How many threads? ";
        unsigned int n;
        std::cin >> n;
        boost::barrier bar(n);
        std::vector<task*> tasks;
        boost::thread_group tg;
        sum = 0;
        for(unsigned int i = 0; i < n; ++i)
        {
    	tasks.push_back( new task(i,bar) );
    	tg.create_thread( *(tasks.back()) );
        }
        tg.join_all();
        for(unsigned int i = 0; i < tasks.size(); ++i)
        {
    	std::cout << tasks[i]->total << std::endl;
    	delete tasks[i];
        }
    }
    Regardless of what a task passes to f, total is always zero. Even having f return a constant yields the same behavior. Is the act of returning causing a problem?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #2

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    I don't understand.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You making a copy of task. The thread is operating on the copy.

    gg

  5. #5
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Well there it is. Thanks.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 10-29-2008, 06:33 AM
  2. Homework help
    By mkdl750 in forum C Programming
    Replies: 45
    Last Post: 07-17-2008, 09:44 PM
  3. weird checksum function
    By sagitt13 in forum C Programming
    Replies: 7
    Last Post: 10-31-2006, 01:25 AM
  4. Craps Program with Local Variables
    By tigrfire in forum C Programming
    Replies: 12
    Last Post: 11-09-2005, 09:01 AM
  5. Return Statement
    By Daveo in forum C Programming
    Replies: 21
    Last Post: 11-09-2004, 05:14 AM