使用 boost 库的多线程
希望同时多次调用一个函数。 我希望使用线程来调用一个函数,以充分利用机器的能力。 这是一台8核机器,我的要求是机器cpu使用率从10%到100%或更多。
我的要求是使用 boost 类。 有什么方法可以使用 boost 线程或线程池库来完成此任务吗? 或者还有其他方法可以做到吗?
另外,如果我必须每次调用具有不同参数的多个函数(使用单独的线程),那么最好的方法是什么? [使用 boost 或不使用 boost] 又如何?
#include <iostream>
#include <fstream>
#include <string.h>
#include <time.h>
#include <boost/thread/mutex.hpp>
#include <boost/bind.hpp>
using namespace std;
using boost::mutex;
using boost::thread;
int threadedAPI1( );
int threadedAPI2( );
int threadedAPI3( );
int threadedAPI4( );
int threadedAPI1( ) {
cout << "Thread0" << endl;
}
int threadedAPI2( ) {
cout << "Thread1" << endl;
}
int threadedAPI3( ) {
cout << "Thread2" << endl;
}
int threadedAPI4( ) {
cout << "Thread3" << endl;
}
int main(int argc, char* argv[]) {
boost::threadpool::thread_pool<> threads(4);
// start a new thread that calls the "threadLockedAPI" function
threads.schedule(boost::bind(&threadedAPI1,0));
threads.schedule(boost::bind(&threadedAPI2,1));
threads.schedule(boost::bind(&threadedAPI3,2));
threads.schedule(boost::bind(&threadedAPI4,3));
// wait for the thread to finish
threads.wait();
return 0;
}
以上不起作用,我不确定为什么? :-(
Wish to simultaneously call a function multiple times. I wish to use threads to call a function which will utilize the machines capability to the fullest. This is a 8 core machine, and my requirement is to use the machine cpu from 10% to 100% or more.
My requirement is to use the boost class. Is there any way I can accomplish this using the boost thread or threadpool library? Or some other way to do it?
Also, if I have to call multiple functions with different parameters each time (with separate threads), what is the best way to do this? [using boost or not using boost] and how?
#include <iostream>
#include <fstream>
#include <string.h>
#include <time.h>
#include <boost/thread/mutex.hpp>
#include <boost/bind.hpp>
using namespace std;
using boost::mutex;
using boost::thread;
int threadedAPI1( );
int threadedAPI2( );
int threadedAPI3( );
int threadedAPI4( );
int threadedAPI1( ) {
cout << "Thread0" << endl;
}
int threadedAPI2( ) {
cout << "Thread1" << endl;
}
int threadedAPI3( ) {
cout << "Thread2" << endl;
}
int threadedAPI4( ) {
cout << "Thread3" << endl;
}
int main(int argc, char* argv[]) {
boost::threadpool::thread_pool<> threads(4);
// start a new thread that calls the "threadLockedAPI" function
threads.schedule(boost::bind(&threadedAPI1,0));
threads.schedule(boost::bind(&threadedAPI2,1));
threads.schedule(boost::bind(&threadedAPI3,2));
threads.schedule(boost::bind(&threadedAPI4,3));
// wait for the thread to finish
threads.wait();
return 0;
}
The above is not working and I am not sure why? :-(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议您阅读您使用的函数的文档。 从 James Hopkin 的回答中的评论来看,您似乎不知道 boost::bind 的作用,而只是复制粘贴了代码。
boost::bind 接受一个函数(称为 f),以及可选的多个参数,并返回一个函数,该函数在调用时使用指定的参数调用 f。
也就是说,boost::bind(threadedAPI1, 0)()(创建一个不带参数的函数,并使用参数 0 调用 threadedAPI1(),然后调用它)相当于
threadedAPI1(0)
。由于您的 threadedAPI 函数实际上并不接受任何参数,因此您无法向它们传递任何参数。 这只是基础的 C++。 您不能调用
threadedAPI1(0)
,而只能调用threadedAPI1()
,但是当您调用该函数时,您会尝试(通过 boost::bind)传递整数 0 作为参数。因此,您的问题的简单答案就是简单地定义 threadedAPI1,如下所示:
但是,避免 boost::bind 调用的一种方法是在启动线程时调用函子而不是自由函数。 声明一个类似这样的类:
最后,根据您的需要,普通线程可能比线程池更适合。 一般来说,线程池只运行有限数量的线程,因此它可能会将一些任务排队,直到其中一个线程完成执行。 它主要适用于有许多短期任务的情况。
如果您有固定数量的较长持续时间的任务,则为每个任务创建专用线程可能是可行的方法。
I suggest that you read up on the documentation for the functions you use. From your comment in James Hopkin's answer, it seems like you don't know what boost::bind does, but simply copy-pasted the code.
boost::bind takes a function (call it f), and optionally a number of parameters, and returns a function which, when called, calls f with the specified parameters.
That is,
boost::bind(threadedAPI1, 0)()
(creating a function which takes no arguments and calls threadedAPI1() with the argument 0, and then calling that) is equivalent tothreadedAPI1(0)
.Since your threadedAPI functions don't actually take any parameters, you can't pass any arguments to them. That is just fundamental C++. You can't call
threadedAPI1(0)
, but onlythreadedAPI1()
, and yet when you call the function, you try (via boost::bind) to pass the integer 0 as an argument.So the simple answer to your question is to simply define threadedAPI1 as follows:
However, one way to avoid the boost::bind calls is to call a functor instead of a free function when launching the thread. Declare a class something like this:
Finally, depending on what you need, plain threads may be better suited than a threadpool. In general, a thread pool only runs a limited number of threads, so it may queue up some tasks until one of its threads finish executing. It is mainly intended for cases where you have many short-lived tasks.
If you have a fixed number of longer-duration tasks, creating a dedicated thread for each may be the way to go.
您将参数绑定到不带参数的函数:
如果没有参数,则直接传递函数:
You're binding parameters to functions that don't take parameters:
Just pass the function directly if there are no parameters:
如果您对高效使用处理器感兴趣,那么您可能需要考虑 intels 线程构建块 http://www.intel.com/cd/software/products/asmo-na/eng/294797.htm。 我相信它是专门为利用多核处理器而设计的,而增强线程则将控制权留给用户(即,与双核相比,TBB 在四核上的线程不同)。
至于您的代码,您正在绑定不将参数传递给参数的函数。 为什么? 您可能还想检查计划中的返回代码。
If your interest is in using your processor effeciently then you might want to consider intels thread building blocks http://www.intel.com/cd/software/products/asmo-na/eng/294797.htm. I believe it is designed specifically to utilise multi core processors while boost threads leaves control up to the user (i.e. TBB will thread differently on a quad core compared to a dual core).
As for your code you are binding functions which don't take parameters to a parameter. Why? You might also want to check the return code from schedule.