调用传递给应用程序的不同线程数的函数

发布于 2024-07-17 20:06:37 字数 968 浏览 8 评论 0原文

我有一个函数,每次需要使用不同数量的线程调用(正在进行一些性能计算,因此需要知道性能何时开始恶化)。 示例如下:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怪异←思 2024-07-24 20:06:37

如果我理解正确,你真正想做的是这样的:

int main() 
{
    int nbThreads = 20;
    boost::threadpool::thread_pool<> threads(nbThreads);

    for(int threadId = 0; threadId <= nbThreads ; ++threadId)
    {
        threads.schedule(getTime);
    }
    threads.wait();  
}

如果我是对的,你不需要在这里使用 boost::bind 。

请注意,Boost.ThreadPool 还不是 Boost 的一部分(还不是吗?)。 它将进行审核,但尚未计划审核日期。

If i understand correctly, what you really want to do is something like this :

int main() 
{
    int nbThreads = 20;
    boost::threadpool::thread_pool<> threads(nbThreads);

    for(int threadId = 0; threadId <= nbThreads ; ++threadId)
    {
        threads.schedule(getTime);
    }
    threads.wait();  
}

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.

夏了南城 2024-07-24 20:06:37

使用 OpenMP 之类的东西怎么样? 如果可扩展性是您的目标,那么它可能是更好的选择。

What about using something like OpenMP? If scalability is your goal, it's probably a better choice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文