Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用传递给应用程序的不同线程数调用函数_C++_Multithreading_Boost Thread_Boost Bind - Fatal编程技术网

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

C++ 使用传递给应用程序的不同线程数调用函数,c++,multithreading,boost-thread,boost-bind,C++,Multithreading,Boost Thread,Boost Bind,我有一个函数,每次需要调用不同数量的线程(我正在进行一些性能计算,所以需要知道性能何时开始恶化)。举例如下: getTime() { return 0; } int main() { boost::threadpool::thread_pool<> threads(nThreads); for(int j = 0; j <= nLines; j++){ threads.schedule(boost::bind(&getTime

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

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();
}
getTime(){
返回0;
}
int main(){
boost::threadpool::thread\u池线程(nThreads);

对于(int j=0;j如果我理解正确,您真正想要做的是这样的:

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

    for(int threadId = 0; threadId <= nbThreads ; ++threadId)
    {
        threads.schedule(getTime);
    }
    threads.wait();  
}
intmain()
{
int=20;
boost::threadpool::thread\u池线程(nbThreads);

对于(int-threadId=0;threadId使用类似的东西怎么样?如果可伸缩性是您的目标,那么它可能是一个更好的选择。

对不起,我的错。没有声明我希望为多个不同的值运行进程,这些值存储在该行变量中,并在getTime()中初始化)。因此需要一个for循环。你所给出的将意味着在我给出的一个for循环中有另一个for循环。如果错误,请纠正我。对不起,我猜我仍然没有正确理解。我所理解的是你想要做一些工作(我不知道确切的内容)并在提供的线程数下分发。这项工作似乎是对getTime函数的nbLines调用,但我不确定。我不知道您希望如何在所有这些中使用nbLines。请尝试更精确一些!我已编辑了主要问题。请告知是否仍不清楚要求。您可以发布一些(接近)吗只在一个线程内执行某些工作的工作代码?这会更清楚。。。
int main() 
{
    int nbThreads = 20;
    boost::threadpool::thread_pool<> threads(nbThreads);

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