0

Note I have already looked at the answer found here: Return Double from Boost thread, however the proposed solution doesn't work for me.

I have the following tid-bits of source code

   void run(int tNumber, std::vector<char *>files, std::map<std::basic_string,float>word_count)
    {

    boost::thread_group threads;

    std::map<std::basic_string,float> temp;

    for(int i = 0; i < tNumber; ++i)
        threads.create_thread(boost::bind(mtReadFile,files[i],boost::ref(word_count)));


    threads.join_all()
    }

This is the function that creates new threads for the calling process. These threads then call an instance of mtReadFile.

    void mtReadFile(char *filename, std::map<std::basic_string,float> word_count)
    {
          //function like things
    }

What I need to happen is word_count be returned from each thread to the calling process. I have tried the boost::ref in hopes of getting around the fact that boost threads copy all arguments to thread storage, but it has't worked for me.

4

1 回答 1

0

Your word_count parameter is passed by value, not by reference:

void mtReadFile(char *filename, std::map<std::basic_string,float> word_count)

instead of

void mtReadFile(char *filename, std::map<std::basic_string,float> &word_count)

This would fail to work even in the single-threaded case, which you should definitely test before attempting the more complex multithreading.

The function needs to accept a reference, AND you need boost::ref() to prevent boost::thread() from copying the parameter before the function invocation.

于 2013-09-09T20:59:58.600 回答