Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include "json.hpp"
  4. #include <fstream>
  5. #include <vector>
  6. #include <mutex>
  7. #include <thread>
  8. #include <chrono>
  9. #include "custom_round.h"
  10.  
  11. using json = nlohmann::json;
  12.  
  13. const std::string kDataFolder = "data/";
  14. const std::string kDataFiles[] = {
  15. "dat_1.json",
  16. "dat_2.json",
  17. "dat_3.json"
  18. };
  19.  
  20. // Just to not a mistsake by writing string directly
  21. #define DATA_TITLE "data"
  22. #define DATA_TITLE_MANUF "manufacturer"
  23. #define DATA_TITLE_DIAM "diameter"
  24. #define DATA_TITLE_FLEN "focalLength"
  25. #define NUMBER_OF_THREADS std::thread::hardware_concurrency()
  26. #define INPUT_ARRAY_SIZE 10
  27. #define THREAD_SLEEP_TIME 100
  28.  
  29. struct CameraInfo {
  30. int focalLength;
  31. double diameter;
  32. std::string manufacturer;
  33. int debugNumber;
  34.  
  35. const bool isValid() const {
  36. return !(focalLength <= 0 || diameter <= 0 || manufacturer == "NULL");
  37. }
  38. };
  39.  
  40. struct SingleResult {
  41. int threadID; // Is this needed?
  42. double fNumber;
  43. std::string entryTitle;
  44. };
  45.  
  46. class Monitor {
  47. public:
  48. bool allDataInserted;
  49.  
  50. private:
  51. CameraInfo inputDataQueue[INPUT_ARRAY_SIZE];
  52. std::vector<SingleResult> threadResults;
  53. std::mutex mtx;
  54. int availableEntry = 0;
  55.  
  56. public:
  57. Monitor() { allDataInserted = false; }
  58. ~Monitor() {}
  59.  
  60. void InsertData(const CameraInfo newData) {
  61. while(availableEntry == INPUT_ARRAY_SIZE){
  62. std::this_thread::sleep_for(std::chrono::milliseconds(THREAD_SLEEP_TIME));
  63. }
  64. mtx.lock(); // kinda unsafe in case of exception mutex is never released
  65. inputDataQueue[availableEntry] = newData;
  66. availableEntry++;
  67. mtx.unlock();
  68. }
  69.  
  70. const CameraInfo GetData() {
  71. while(availableEntry <= 0){
  72. if(allDataInserted) return {-1, -1, "NULL", -1};
  73. std::cout << "Waiting for data...\n";
  74. std::this_thread::sleep_for(std::chrono::milliseconds(THREAD_SLEEP_TIME));
  75. }
  76. mtx.lock();
  77. CameraInfo t = inputDataQueue[availableEntry - 1];
  78. availableEntry--;
  79. mtx.unlock();
  80. return t;
  81. }
  82.  
  83. void InsertResult(SingleResult result) {
  84. mtx.lock();
  85. if(threadResults.size() == 0) {
  86. threadResults.push_back(result);
  87. }
  88. else {
  89. for(auto it = threadResults.begin(); it != threadResults.end(); it++){
  90. if((*it).fNumber > result.fNumber){
  91. threadResults.insert(it, result);
  92. break;
  93. }
  94. }
  95. }
  96. mtx.unlock();
  97. }
  98.  
  99. const std::vector<SingleResult> GetAllResults() const {
  100. return threadResults;
  101. }
  102. };
  103. Monitor monitor;
  104.  
  105.  
  106. json ReadFile(int fileNumber) {
  107. std::ifstream dataFile("./" + kDataFolder + kDataFiles[fileNumber]);
  108. json j;
  109. dataFile >> j;
  110. dataFile.close();
  111. return j;
  112. }
  113.  
  114. void ExecuteSingleThread(int thrdID) {
  115. while(true) {
  116. SingleResult result;
  117. result.threadID = thrdID;
  118. CameraInfo camInfo = monitor.GetData();
  119. if(!camInfo.isValid()){
  120. break;
  121. }
  122. result.fNumber = camInfo.focalLength / camInfo.diameter;
  123. result.entryTitle = camInfo.manufacturer + " f=" + std::to_string(round(result.fNumber, 3));
  124.  
  125. std::string thingToPrint = "[THRD:" +std::to_string(thrdID) + ", DATA_ID: " + std::to_string(camInfo.debugNumber) + "] " + result.entryTitle + "\n";
  126. std::cout << thingToPrint;
  127.  
  128. monitor.InsertResult(result);
  129. }
  130. }
  131.  
  132. int main() {
  133. json j = ReadFile(0);
  134. // Can I just store it in json?
  135. // If not here's storing into custom struct
  136. const int nOfElements = j[DATA_TITLE].size();
  137. CameraInfo camInfo[nOfElements];
  138. for(int i = 0; i < nOfElements; i++) {
  139. camInfo[i].focalLength = j[DATA_TITLE][i][DATA_TITLE_FLEN];
  140. camInfo[i].diameter = j[DATA_TITLE][i][DATA_TITLE_DIAM];
  141. camInfo[i].manufacturer = j[DATA_TITLE][i][DATA_TITLE_MANUF];
  142. camInfo[i].debugNumber = i;
  143. }
  144.  
  145. std::thread workerThreads[NUMBER_OF_THREADS];
  146. for(int i = 0; i < NUMBER_OF_THREADS; i++) {
  147. workerThreads[i] = std::thread(ExecuteSingleThread, i);
  148. }
  149. for(int i = 0; i < nOfElements; i++) {
  150. monitor.InsertData(camInfo[i]);
  151. }
  152. monitor.allDataInserted = true;
  153. std::cout << "All data is inserted\n";
  154. for(int i = 0; i < NUMBER_OF_THREADS; i++) {
  155. workerThreads[i].join();
  156. }
  157.  
  158. return 0;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement