Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include "json.hpp"
- #include <fstream>
- #include <vector>
- #include <mutex>
- #include <thread>
- #include <chrono>
- #include "custom_round.h"
- using json = nlohmann::json;
- const std::string kDataFolder = "data/";
- const std::string kDataFiles[] = {
- "dat_1.json",
- "dat_2.json",
- "dat_3.json"
- };
- // Just to not a mistsake by writing string directly
- #define DATA_TITLE "data"
- #define DATA_TITLE_MANUF "manufacturer"
- #define DATA_TITLE_DIAM "diameter"
- #define DATA_TITLE_FLEN "focalLength"
- #define NUMBER_OF_THREADS std::thread::hardware_concurrency()
- #define INPUT_ARRAY_SIZE 10
- #define THREAD_SLEEP_TIME 100
- struct CameraInfo {
- int focalLength;
- double diameter;
- std::string manufacturer;
- int debugNumber;
- const bool isValid() const {
- return !(focalLength <= 0 || diameter <= 0 || manufacturer == "NULL");
- }
- };
- struct SingleResult {
- int threadID; // Is this needed?
- double fNumber;
- std::string entryTitle;
- };
- class Monitor {
- public:
- bool allDataInserted;
- private:
- CameraInfo inputDataQueue[INPUT_ARRAY_SIZE];
- std::vector<SingleResult> threadResults;
- std::mutex mtx;
- int availableEntry = 0;
- public:
- Monitor() { allDataInserted = false; }
- ~Monitor() {}
- void InsertData(const CameraInfo newData) {
- while(availableEntry == INPUT_ARRAY_SIZE){
- std::this_thread::sleep_for(std::chrono::milliseconds(THREAD_SLEEP_TIME));
- }
- mtx.lock(); // kinda unsafe in case of exception mutex is never released
- inputDataQueue[availableEntry] = newData;
- availableEntry++;
- mtx.unlock();
- }
- const CameraInfo GetData() {
- while(availableEntry <= 0){
- if(allDataInserted) return {-1, -1, "NULL", -1};
- std::cout << "Waiting for data...\n";
- std::this_thread::sleep_for(std::chrono::milliseconds(THREAD_SLEEP_TIME));
- }
- mtx.lock();
- CameraInfo t = inputDataQueue[availableEntry - 1];
- availableEntry--;
- mtx.unlock();
- return t;
- }
- void InsertResult(SingleResult result) {
- mtx.lock();
- if(threadResults.size() == 0) {
- threadResults.push_back(result);
- }
- else {
- for(auto it = threadResults.begin(); it != threadResults.end(); it++){
- if((*it).fNumber > result.fNumber){
- threadResults.insert(it, result);
- break;
- }
- }
- }
- mtx.unlock();
- }
- const std::vector<SingleResult> GetAllResults() const {
- return threadResults;
- }
- };
- Monitor monitor;
- json ReadFile(int fileNumber) {
- std::ifstream dataFile("./" + kDataFolder + kDataFiles[fileNumber]);
- json j;
- dataFile >> j;
- dataFile.close();
- return j;
- }
- void ExecuteSingleThread(int thrdID) {
- while(true) {
- SingleResult result;
- result.threadID = thrdID;
- CameraInfo camInfo = monitor.GetData();
- if(!camInfo.isValid()){
- break;
- }
- result.fNumber = camInfo.focalLength / camInfo.diameter;
- result.entryTitle = camInfo.manufacturer + " f=" + std::to_string(round(result.fNumber, 3));
- std::string thingToPrint = "[THRD:" +std::to_string(thrdID) + ", DATA_ID: " + std::to_string(camInfo.debugNumber) + "] " + result.entryTitle + "\n";
- std::cout << thingToPrint;
- monitor.InsertResult(result);
- }
- }
- int main() {
- json j = ReadFile(0);
- // Can I just store it in json?
- // If not here's storing into custom struct
- const int nOfElements = j[DATA_TITLE].size();
- CameraInfo camInfo[nOfElements];
- for(int i = 0; i < nOfElements; i++) {
- camInfo[i].focalLength = j[DATA_TITLE][i][DATA_TITLE_FLEN];
- camInfo[i].diameter = j[DATA_TITLE][i][DATA_TITLE_DIAM];
- camInfo[i].manufacturer = j[DATA_TITLE][i][DATA_TITLE_MANUF];
- camInfo[i].debugNumber = i;
- }
- std::thread workerThreads[NUMBER_OF_THREADS];
- for(int i = 0; i < NUMBER_OF_THREADS; i++) {
- workerThreads[i] = std::thread(ExecuteSingleThread, i);
- }
- for(int i = 0; i < nOfElements; i++) {
- monitor.InsertData(camInfo[i]);
- }
- monitor.allDataInserted = true;
- std::cout << "All data is inserted\n";
- for(int i = 0; i < NUMBER_OF_THREADS; i++) {
- workerThreads[i].join();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement