Created
November 26, 2019 00:12
-
-
Save blewert/c194d6534047a86affa2f8cee3281b48 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// threaded-file-read.cpp : This file contains the 'main' function. Program execution begins and ends there. | |
// | |
#include <thread> | |
#include <iostream> | |
#include <vector> | |
#include <functional> | |
#include <fstream> | |
#include <string> | |
#define _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING | |
#include <experimental/filesystem> | |
#include <filesystem> | |
namespace fs = std::experimental::filesystem::v1; | |
const int THREAD_POOL_SIZE = 32; | |
class FileThread | |
{ | |
public: | |
FileThread(int threadID, void (*function)(FileThread*), std::string path) : threadID(threadID), function(function), path(path) | |
{ | |
thread = std::thread(function, this); | |
} | |
void (*function)(FileThread*); | |
std::thread thread; | |
std::string path; | |
bool complete = false; | |
int threadID = 0; | |
}; | |
void threadFunc(FileThread* callThread) | |
{ | |
int a = 0; | |
std::ifstream ifs; | |
ifs.open(callThread->path, std::ifstream::in); | |
std::string line; | |
while (ifs.good()) | |
{ | |
std::getline(ifs, line); | |
} | |
ifs.close(); | |
callThread->complete = true; | |
} | |
void initializeThreadPool(std::vector<FileThread*>& pool, const std::vector<std::string>& filePaths) | |
{ | |
for (int i = 0; i < THREAD_POOL_SIZE; i++) | |
pool.push_back(new FileThread(i, threadFunc, filePaths[i])); | |
} | |
int main() | |
{ | |
std::vector<FileThread*> pool; | |
std::vector<std::string> filePaths; | |
std::string dirPath = "D:\\git\\study1-logs\\analysis\\raw_telemetry\\m"; | |
int completeCount = 0; | |
int fileCount = 0; | |
for (const auto& entry : fs::directory_iterator(dirPath)) | |
filePaths.push_back(entry.path().string()); | |
initializeThreadPool(pool, filePaths); | |
int numberComplete = 0; | |
while (numberComplete < pool.size()) | |
{ | |
numberComplete = 0; | |
for (int i = 0; i < pool.size(); i++) | |
{ | |
if (pool[i]->complete) | |
{ | |
numberComplete++; | |
std::cout << "x"; | |
} | |
else | |
std::cout << "-"; | |
} | |
std::cout << std::endl; | |
} | |
for (int i = 0; i < pool.size(); i++) | |
pool[i]->thread.join(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment