using namespace std;
using namespace boost::threadpool;// Some example tasks
void first_task()
{
cout << "first task is running\n" ;
}void second_task()
{
cout << "second task is running\n" ;
}int task_with_parameter(int value)
{
cout << "task_with_parameter(" << value << ")\n";
return 0;
}int task_int_23()
{
cout<<"task_int_23()\n";
return 23;
}
int _tmain(int argc, _TCHAR* argv[])
{
pool tp(2); // Add some tasks to the pool.
tp.schedule(&first_task);
tp.schedule(&second_task);
tp.schedule(boost::bind(task_with_parameter, 4)); // Wait until all tasks are finished.
tp.wait(); future<int> res = schedule(tp, &task_int_23);
res.wait();
cout<<"get res value:"<<res.get()<<endl;
//schedule(tp, boost::bind(task_with_parameter, 4)); //加上这句编译不过,请问应该怎么使用,在这里我要给线程传递参数和取得线程的返回值
return 0;
}
using namespace boost::threadpool;// Some example tasks
void first_task()
{
cout << "first task is running\n" ;
}void second_task()
{
cout << "second task is running\n" ;
}int task_with_parameter(int value)
{
cout << "task_with_parameter(" << value << ")\n";
return 0;
}int task_int_23()
{
cout<<"task_int_23()\n";
return 23;
}
int _tmain(int argc, _TCHAR* argv[])
{
pool tp(2); // Add some tasks to the pool.
tp.schedule(&first_task);
tp.schedule(&second_task);
tp.schedule(boost::bind(task_with_parameter, 4)); // Wait until all tasks are finished.
tp.wait(); future<int> res = schedule(tp, &task_int_23);
res.wait();
cout<<"get res value:"<<res.get()<<endl;
//schedule(tp, boost::bind(task_with_parameter, 4)); //加上这句编译不过,请问应该怎么使用,在这里我要给线程传递参数和取得线程的返回值
return 0;
}
当函数返回值为void时,schedule(tp, boost::bind <int>(task_with_parameter, 4));没问题,
返回值为int时则有问题