调用传递给应用程序的不同线程数的函数
我有一个函数,每次需要使用不同数量的线程调用(正在进行一些性能计算,因此需要知道性能何时开始恶化)。 示例如下:
getTime() {
return 0;
}
int main() {
boost::threadpool::thread_pool<> threads(nThreads);
for(int j = 0; j <= nLines; j++){
threads.schedule(boost::bind(&getTime, nThreads, 1));
}
threads.wait();
}
其中, nThreads:在命令行给出的值
我的问题是,这会给我想要的结果吗?每次程序访问 for 循环时,都会用“nThreads”调用“getTime”函数吗? 或者我是否需要其他方法来找出相同的信息?
我真正想做的是:(
boost::threadpool::thread_pool<> threads(nThreads);
// start a new thread that calls the "getTime" function
for(int j = 0; j <= nLines; j++){
//threads.schedule(boost::bind(&getTime, nThreads, 1));
threads.schedule(boost::bind(&getTime, 0, nLines, pc));
}
不确定上面哪一个是正确的。)
getTime() 函数将以我从文本文件中获取的指定行数运行,并将每一行提供给 api,我想计算其表现。 但这与我的问题无关。
我希望每次调用具有不同线程数的函数,并计算每个线程完成所需的时间。 1 个线程花费的总时间是多少,2 个线程完成所需的总时间是多少,等等。
I have a function which needs to be invoked with a different number of threads each time (am doing some performance calculation, so need to know when the performance starts deteriorating). Example is given below:
getTime() {
return 0;
}
int main() {
boost::threadpool::thread_pool<> threads(nThreads);
for(int j = 0; j <= nLines; j++){
threads.schedule(boost::bind(&getTime, nThreads, 1));
}
threads.wait();
}
Where,
nThreads: A value given at the command line
My question is, would this give me the desired result, as to would this call the 'getTime' function with 'nThreads' each time the for loop is accessed by the program? Or do I need some other method to find out the same?
what i really want to do is this:
boost::threadpool::thread_pool<> threads(nThreads);
// start a new thread that calls the "getTime" function
for(int j = 0; j <= nLines; j++){
//threads.schedule(boost::bind(&getTime, nThreads, 1));
threads.schedule(boost::bind(&getTime, 0, nLines, pc));
}
(not sure which of the above is correct.)
the getTime() function is to be run with a specified number of lines which i get from a text file and give each line to a api, whose performance i wish to calculate. but this is unrelated to the question i have.
i wish to call a function with different number of threads each time and calculate how much time it took each thread to finish. what was the total time taken with 1 thread, what was the total time taken by 2 threads to finish, etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我理解正确,你真正想做的是这样的:
如果我是对的,你不需要在这里使用 boost::bind 。
请注意,Boost.ThreadPool 还不是 Boost 的一部分(还不是吗?)。 它将进行审核,但尚未计划审核日期。
If i understand correctly, what you really want to do is something like this :
If i am right, you don't need to use boost::bind here.
Note that Boost.ThreadPool is not part of Boost (yet? it ). It will be reviewed, but no review date has been planned yet.
使用 OpenMP 之类的东西怎么样? 如果可扩展性是您的目标,那么它可能是更好的选择。
What about using something like OpenMP? If scalability is your goal, it's probably a better choice.