Markopy
Utilizing Markov Models for brute forcing attacks
threadSharedListHandler.h
Go to the documentation of this file.
1 /** @file threadSharedListHandler.h
2  * @brief Thread-safe wrapper for std::ifstream
3  * @authors Ata Hakçıl
4  *
5  * @copydoc Markov::API::Concurrency::ThreadSharedListHandler
6  */
7 
8 #include <string>
9 #include <fstream>
10 #include <mutex>
11 
12 /** @brief Namespace for Concurrency related classes
13 */
14 namespace Markov::API::Concurrency{
15 
16 /** @brief Simple class for managing shared access to file
17  *
18  * This class maintains the handover of each line from a file to multiple threads.
19  *
20  * When two different threads try to read from the same file while reading a line isn't completed, it can have unexpected results.
21  * Line might be split, or might be read twice.
22  * This class locks the read action on the list until a line is completed, and then proceeds with the handover.
23  *
24 */
26 public:
27  /** @brief Construct the Thread Handler with a filename
28  *
29  * Simply open the file, and initialize the locks.
30  *
31  * @b Example @b Use: Simple file read
32  * @code{.cpp}
33  * ThreadSharedListHandler listhandler("test.txt");
34  * std::string line;
35  * std::cout << listhandler->next(&line) << "\n";
36  * @endcode
37  *
38  * @b Example @b Use: Example use case from MarkovPasswords showing multithreaded access
39  * @code{.cpp}
40  * void MarkovPasswords::Train(const char* datasetFileName, char delimiter, int threads) {
41  * ThreadSharedListHandler listhandler(datasetFileName);
42  * auto start = std::chrono::high_resolution_clock::now();
43  *
44  * std::vector<std::thread*> threadsV;
45  * for(int i=0;i<threads;i++){
46  * threadsV.push_back(new std::thread(&MarkovPasswords::TrainThread, this, &listhandler, datasetFileName, delimiter));
47  * }
48  *
49  * for(int i=0;i<threads;i++){
50  * threadsV[i]->join();
51  * delete threadsV[i];
52  * }
53  * auto finish = std::chrono::high_resolution_clock::now();
54  * std::chrono::duration<double> elapsed = finish - start;
55  * std::cout << "Elapsed time: " << elapsed.count() << " s\n";
56  *
57  * }
58  *
59  * void MarkovPasswords::TrainThread(ThreadSharedListHandler *listhandler, const char* datasetFileName, char delimiter){
60  * char format_str[] ="%ld,%s";
61  * format_str[2]=delimiter;
62  * std::string line;
63  * while (listhandler->next(&line)) {
64  * long int oc;
65  * if (line.size() > 100) {
66  * line = line.substr(0, 100);
67  * }
68  * char* linebuf = new char[line.length()+5];
69  * sscanf_s(line.c_str(), format_str, &oc, linebuf, line.length()+5);
70  * this->AdjustEdge((const char*)linebuf, oc);
71  * delete linebuf;
72  * }
73  * }
74  * @endcode
75  *
76  * @param filename Filename for the file to manage.
77  */
78  ThreadSharedListHandler(const char* filename);
79 
80  /** @brief Read the next line from the file.
81  *
82  * This action will be blocked until another thread (if any) completes the read operation on the file.
83  *
84  * @b Example @b Use: Simple file read
85  * @code{.cpp}
86  * ThreadSharedListHandler listhandler("test.txt");
87  * std::string line;
88  * std::cout << listhandler->next(&line) << "\n";
89  * @endcode
90  *
91  */
92  bool next(std::string* line);
93 
94 private:
95  std::ifstream listfile;
96  std::mutex mlock;
97 };
98 
99 };