Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multithreading does not change speed

I am currently working on speeding up one of my c++ program, and trying to discover the beautiful world of multithreading.

std::vector<std::thread> threadPool;
threadPool.clear();
for(int t = 0; t < nbThreads; ++t){
    threadPool.push_back(std::thread(myFunction, std::ref(rep), std::ref(branch1));
}
for(auto t = threadPool.begin(); t < threadPool.end() ; ++t){
    t->join();
}

What I don't understand is that whatever the number of threads I use, the speed does not change! (about 30sec).

To sum up : If my code is wrong, what is wrong? If it is not wrong, in what case should I use multithreading to speed up the computation?

Edit : I run on a i7-4810MQ machine

Edit 2 : here is what myFunction does (it parses xml files)

void myfunction(DIR*& rep, struct dirent*& branch1){
mtx.lock();
while ((branch1 = readdir(rep)) != NULL){
    mtx.unlock()

    TiXmlDocument doc(branch1->d_name);
    if(doc.LoadFile()){
            //parse file
    }

    mtx.lock();
}
mtx.unlock();
}
like image 506
Arcyno Avatar asked Apr 09 '26 09:04

Arcyno


1 Answers

There's a good chance that your code is limited by file I/O. There's also a good chance that your XML parser makes lots of memory allocations, and memory allocations may slow down if many threads allocate memory simultaneously.

like image 144
gnasher729 Avatar answered Apr 11 '26 22:04

gnasher729



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!