simple thread pool test

saya mencoba simple thread pool menggunakan std::thread C++ 11 dan sepertinya bekerja sesuai keinginan dari test yang saya lakukan (warning : code dibawah ini trivial).  jumlah thread saya harcode berjumlah 2 :p.  untuk mengetest thread pool, saya beri task meng-copy files berjumlah 100 (nama file : 1- 100) .  compiler: vs studio express 2012 . header :

#include #include #include #include

[sourcecode language=“cpp”] //kelas worker thread class worker { private: std::vector< std::function > vecFunc; std::mutex m_mutex; public: void addtask(std::function task) { std::lock_guardstd::mutex lockg(m_mutex); vecFunc.push_back(task); } void executeSave() { while(1) { if (vecFunc.size() > 0) { std::lock_guardstd::mutex lockg(m_mutex); for (int i = 0; i < vecFunc.size(); i++) { vecFunc[i](); } vecFunc.clear(); } } } };

// kelas thread pool class FSThreadPool { private: FSThreadPool() { t_iterator =0; m_ThreadCount = 2; }; int m_ThreadCount; std::vector< std::thread* > m_threads; static FSThreadPool* m_Instance; worker m_worker1; worker m_worker2; int t_iterator; std::vectorstd::thread* threadSaving; public: static FSThreadPool* getInstance() { if(m_Instance == NULL) m_Instance = new FSThreadPool(); return m_Instance; } ~FSThreadPool() { delete m_Instance; delete m_threads[0]; delete m_threads[1]; } void Init() { m_threads.push_back( new std::thread( &worker::executeSave,&m_worker1 ) ); m_threads.push_back( new std::thread( &worker::executeSave,&m_worker2 ) ); } void addtask(std::function task) { if(t_iterator==0) m_worker1.addtask(task); else if(t_iterator==1) m_worker2.addtask(task);

    t_iterator++;
    if(t_iterator==2)
        t_iterator=0;
}

}; FSThreadPool* FSThreadPool::m_Instance = NULL;

//=== test thread ====// void Thcopyfile(wchar_t* source,wchar_t* dest) { CopyFile2(source,dest,NULL);

delete source;
delete dest;

}

void fillListFile() { std::vector< char* > vecFname; for(int i=0;i<100;i++) { char* packfname = (char*) malloc(sizeof(char)*256); _itoa(i,packfname,10); vecFname.push_back(packfname); }

std::vector<char*> ::iterator it;
for (it=vecFname.begin();it!=vecFname.end();it++)
{
    char* fname = (*it);

    char filePathSRC[256];
    sprintf(filePathSRC, "data_source\\%s", fname);

    char filePathDEST[256];
    sprintf(filePathDEST, "data_dest\\%s", fname);

    wchar_t *w_filePathSRC = 0;
    w_filePathSRC = (wchar_t*) malloc((strlen(filePathSRC) + 1) * sizeof(wchar_t));
    mbstowcs(w_filePathSRC, filePathSRC, strlen(filePathSRC) + 1);

    wchar_t *w_filePathDEST = 0;
    w_filePathDEST = (wchar_t*) malloc((strlen(filePathDEST) + 1) * sizeof(wchar_t));
    mbstowcs(w_filePathDEST, filePathDEST, strlen(filePathDEST) + 1);

#define THREADCOPY #ifndef THREADCOPY CopyFile2(w_filePathSRC,w_filePathDEST,NULL); delete w_filePathSRC; delete w_filePathDEST; #else FSThreadPool::getInstance()->addtask( std::bind(&Thcopyfile,w_filePathSRC,w_filePathDEST) ); #endif } }

int main() { FSThreadPool::getInstance()->Init(); std::cout<<” start copying ….”<<std::endl; long long startTime = clock(); fillListFile(); long long TotalTime = clock() - startTime; std::cout<<” end copying ….”<<std::endl; std::cout<<” time : “<<TotalTime<<std::endl;

while(1) {
    std::cout<<"in main loop"<<std::endl;
}

} [/sourcecode]

// weekend 21 sept 2012, // yogyakarta

comments powered by Disqus