-2

Suppose I have a function which returns a map like:

std::map<std::string,std::string>  functionname(string abc123)

How can I pass different string to same function in separate threads using boost thread??(value returned is stored in different variables)

psyche
  • 443
  • 1
  • 5
  • 14
  • 1
    Are you asking how to call functions? How to start threads? What? – Lightness Races in Orbit Jan 17 '13 at 19:30
  • @LightnessRacesinOrbit actually both. – psyche Jan 17 '13 at 19:35
  • Is there no shared state in the `functionname()` function -- (is that function itself thread safe)? – Chad Jan 17 '13 at 19:35
  • @PragneshPatel: Then that's two very broad questions. What books and resources are you using? What do you have so far? It's not clear what the problem is here other than that you haven't done enough research yet :) – Lightness Races in Orbit Jan 17 '13 at 19:38
  • @Chad I dont know exactly what you mean(new to multithreading) but let me clear your doubt from what i understand this function is completely not dependent on any variable outside function other than what is inputted to it that is abc123(string variable) uses boost string and regex functions and returns an std::map. – psyche Jan 17 '13 at 19:42
  • @LightnessRacesinOrbit I have read some what basics from what i got on net but quite confused on what i have read .Called void functions who dont have any input works fine but not with functions having input and returning values.I actually require a 101 on this. :) – psyche Jan 17 '13 at 19:45
  • @LightnessRacesinOrbit It would be helpful if someone could provide links to learn boost::thread systematically – psyche Jan 17 '13 at 19:57
  • Stack Overflow is not a place to request tutorials, really. You should ask a specific question about a [pre-existing minimal example](http://sscce.org) of code. – Lightness Races in Orbit Jan 17 '13 at 20:01
  • @LightnessRacesinOrbit I wanted a startup link to read in depth of boost::thread in a systematic way if it is against stack overflow rules I apologise for it. – psyche Jan 17 '13 at 20:09
  • @PragneshPatel: Yeah that's more suited to a forum, or [chat](http://chat.stackoverflow.com/rooms/10/loungec). This is a Q&A for specific questions. – Lightness Races in Orbit Jan 17 '13 at 20:16
  • @PragneshPatel: [Boost documentation](http://www.boost.org/doc/libs/1_52_0/doc/html/thread.html) is a good place "to read in depth of boost::thread in a systematic way". – Yakov Galka Jan 17 '13 at 22:28

1 Answers1

2
int main()
{
    string param1 = ...;
    string param2 = ...;

    typedef std::map<std::string,std::string> RetT;
    boost::future<RetT> f1 = boost::async(boost::launch::async,
        boost::bind(functionname, param1));
    boost::future<RetT> f2 = boost::async(boost::launch::async,
        boost::bind(functionname, param2));

    // here they run....

    RetT r1 = f1.get(); // waits for f1
    RetT r2 = f2.get(); // waits for f2

    // Here we have the results in r1 and r2
}
Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
  • what are header files required to be included for boost::future and boost::async getting error: 'future' is not a member of 'boost' simlarly error for async is not a member of 'boost' – psyche Jan 18 '13 at 09:07
  • @PragneshPatel: `#include ` – Yakov Galka Jan 18 '13 at 09:11
  • futures.hpp not present future.hpp is present and already included that header file(before asking for help) but still error is coming – psyche Jan 18 '13 at 09:16
  • @PragneshPatel: what version of boost you have? – Yakov Galka Jan 18 '13 at 09:22
  • I am on windows and mingw gcc4.7 – psyche Jan 18 '13 at 09:25
  • 1
    @PragneshPatel: seems you have an older version. `#include ` and use `boost::shared_future` instead of `boost::future`. – Yakov Galka Jan 18 '13 at 09:28
  • 'future' error gone but 'async' still there. – psyche Jan 18 '13 at 09:34
  • @PragneshPatel: OK, [according to the docs](http://www.boost.org/doc/libs/1_50_0/doc/html/thread/synchronization.html#thread.synchronization.futures) it was not yet implemented in Boost 1.50. Now it is a part of the C++11 standard. Your options are: 1) upgrade to latest boost, 2) use packaged_task instead as described in the documentation I linked, or 3) use the `std::` equivalents which are likely to be there in GCC4.7 when you compile in C++11 experimental mode. – Yakov Galka Jan 18 '13 at 09:39