使用Boost Thread在具有不同输入的多个线程上运行一个函数

using Boost Thread for running a function on multiple threads with different input

本文关键字:运行 线程 函数 一个 Thread Boost 输入 使用      更新时间:2023-10-16

假设我有一个函数,它返回一个映射:

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

我怎么能通过不同的字符串相同的函数在单独的线程使用boost线程?(返回值存储在不同的变量中)

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
}