c++ - Multithreaded MD5 Hashing -
i'm trying multithread program wich take word file hash these , write another.
if make without multithreading it's fast, it's able use 15-20% of cpu , has out 300.000line/s
but when tryied multithreading slow down , start hash @ 17000lines/s, can please me?
thanks
#include <iostream> // std::cout, std::streambuf, std::streamsize #include <fstream> // std::ifstream #include <string> #include <thread> #include "md5.h" using namespace std; static const int num_threads = 10; void call_from_thread(int tid) { cout << "launched thread " << tid << std::endl; int cl = 0; int uscita = 0; int parole = 0; char* contents; ifstream istr("test.txt"); if (istr) { streambuf * pbuf = istr.rdbuf(); streamsize size = pbuf->pubseekoff(0, istr.end); pbuf->pubseekoff(0, istr.beg); // rewind contents = new char[size]; pbuf->sgetn(contents, size); istr.close(); ofstream myfile; myfile.open("out.txt"); { string prova(""); uscita = 0; { if (contents[cl] == '\n') { uscita = 1; } prova += contents[cl]; cl += 1; } while (uscita != 1); parole += 1; //cout << prova << ":" << md5(prova) << endl; myfile << prova << ":" << md5(prova) << endl; } while (parole != 9586054); myfile.close(); } } int main() { thread t[num_threads]; //launch group of threads (int = 0; < num_threads; ++i) { t[i] = thread(call_from_thread, i); } cout << "launched main\n"; //join threads main thread (int = 0; < num_threads; ++i) { t[i].join(); } return 0; }
what see here threads reading same file , doing same work. results in file has read num_threads
times, causing slowdown of num_threads
(since file i/o on critical path) or maybe more because of more cache misses.
you try reading entire file memory, splitting between threads , having each thread process subset of files content.
wiki
Comments
Post a Comment