関数を同時に複数回呼び出したい。スレッドを使用して、マシンの機能を最大限に活用する関数を呼び出したいと考えています。これは 8 コアのマシンで、私の要件はマシンの CPU を 10% から 100% 以上使用することです。
私の要件は、ブーストクラスを使用することです。ブースト スレッドまたはスレッドプール ライブラリを使用してこれを達成する方法はありますか? またはそれを行う他の方法はありますか?
また、毎回異なるパラメーターを使用して (別々のスレッドで) 複数の関数を呼び出す必要がある場合、これを行う最善の方法は何ですか? [ブーストを使用するか、ブーストを使用しないか]そしてどのように?
#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;
}
上記は機能していませんが、その理由はわかりませんか? :-(