C++: Making two loops run simultaneously
Anonymous in /c/coding_help
936
report
What I need:<br><br>I am building a live plotting program in c++. I want to generate random data points and plot them in real time. The problem is that my while loop doesn't run while the plotting continues.<br><br>What I have done so far:<br><br>Here is the code:<br><br>```cpp<br>#include <fstream><br>#include <iostream><br>#include <cmath><br>#include <vector><br>#include <algorithm><br>#include <string><br>#include <algorithm><br>#include <map><br>#include <math.h><br>#include <random><br>#include <chrono><br>#include <thread><br><br>#include <pybind11/embed.h> // must be listed before any other pybind header<br>#include <pybind11/stl.h><br>#include <pybind11/eval.h><br>#include <pybind11/functional.h><br><br>namespace py = pybind11;<br><br>int main(){<br><br> py::initialize();<br><br> <br> std::vector<double> x = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};<br> std::vector<double> y;<br><br> for(int i =0 ; i<100; i++){<br> <br> std::ifstream node_id("nodeid.txt");<br> std::string line;<br> std::string NodeID;<br> std::vector<double> temp_y;<br><br> if(node_id.is_open()){<br><br> while(std::getline(node_id, line)){<br> NodeID = line;<br> }<br> node_id.close();<br> }<br><br> std::ifstream app_id("appid.txt");<br> std::getline(app_id, line);<br> app_id.close();<br><br> std::ifstream file("data.txt");<br> if(file.is_open()){<br><br> while(std::getline(file, line)){<br> line.erase(std::remove(line.begin(), line.end(), ' '), line.end());<br> line.erase(std::remove(line.begin(), line.end(), '\n'), line.end());<br> line.erase(std::remove(line.begin(), line.end(), '\t'), line.end());<br> size_t comma = line.find(',');<br> while(comma != std::string::npos){<br> temp_y.push_back(std::stod(line.substr(0, comma)));<br> line.erase(line.begin(), line.begin() + comma+1);<br> comma = line.find(',');<br> }<br> temp_y.push_back(std::stod(line));<br> }<br><br> file.close();<br><br> for(int z = 0; z < 20; z++){<br> y.push_back(temp_y[z]);<br> }<br><br> py::print("Plotting.....");<br><br> py::module matplotlib = py::module::import("matplotlib.pyplot");<br> py::print("Imported Modules");<br> py::module numpy = py::module::import("numpy");<br> py::print("Plotting");<br> matplotlib.attr("plot")(numpy.attr("asarray")(x), numpy.attr("asarray")(y), nodeid);<br> matplotlib.attr hypnotic_mnemonic ("ion")();<br> matplotlib.attr("pause")(0.0001);<br> matplotlib.attr("clf")();<br> y.clear();<br><br> }<br><br> }<br> <br><br> return 0;<br>}<br>```<br><br>What I need:<br><br>I want to run a function `gen_data()` simultaneously while running the main while loop of the code above. This function has a while loop and I want to make it run alongside the main while loop. I am new to c++ and I am not aware how multithreading works in this environment.<br><br>The function is as follows:<br><br>```cpp<br>void gen_data(){<br> std::ifstream node_id("nodeid.txt");<br> std::string line;<br> std::string NodeID;<br> std::ifstream app_id("appid.txt");<br><br> if(node_id.is_open()){<br> while(std::getline(node_id, line)){<br> NodeID = line;<br> }<br> node_id.close();<br> }<br><br> std::ofstream ptofile_id("nodeid.txt");<br> if(ptofile_id.is_open()){<br> ptofile_id << NodeID;<br> ptofile_id.close();<br> }<br><br> std::ifstream app_id("appid.txt");<br> std::string app_id_string;<br> std::string last_data;<br> std::string temp;<br><br> if(app_id.is_open()){<br><br> while(std::getline(app_id, line)){<br> app_id_string = line;<br> }<br> app_id.close();<br> }<br><br> app_id.open("appid.txt", std::fstream::in | std::fstream::out | std::fstream::ate);<br><br> auto node_pos = app_id.tellg();<br> app_id.seekg(0, std::ios::end);<br> auto app_pos = app_id.tellg();<br> app_id.seekg(0);<br><br> app_id.seekg(- node_pos + app_pos, std::ios::cur);<br><br> while(!app_id.eof()){<br> app_id >> temp;<br> last_data.append(temp);<br> last_data.push_back(' ');<br> }<br><br> last_data.push_back('\n');<br><br> py::print(last_data);<br><br> app_id.clear();<br><br> std::ifstream node_id("nodeid.txt");<br> if(app_id.is_open()) node_id >> NodeID;<br><br> app_id.seekg(- node_pos + app_pos, std::ios::cur);<br><br> app_id.clear();<br><br> while(1){<br> std::ifstream app_id("appid.txt");<br> if(app_id.is_open()){<br><br> app_id.clear();<br> app_id.seekg(- node_pos + app_pos, std::ios::cur);<br><br> std::string line;<br> while(std::getline(app_id, line)){<br> line.erase(std::remove(line.begin(), line.end(), '\n'), line.end());<br> line.erase(std::remove(line.begin(), line.end(), '\t'), line.end());<br> node_id.open("nodeid.txt");<br> node_id >> NodeID;<br> node_id.close();<br><br> std::string temp_string;<br> size_t comma = line.find(',');<br> while(comma != std::string::npos){<br> temp_string.append(NodeID);<br> temp_string.push_back(',');<br> temp_string.append(std::to_string(rand()*1.0/RAND_MAX));<br> temp_string.push_back(',');<br> line.erase(line.begin(), line.begin() + comma + 1);<br> comma = line.find(',');<br> }<br> <br> temp_string.append(NodeID);<br> temp_string.push_back(',');<br> temp_string.append(std::to_string(rand()*1.0/RAND_MAX));<br> temp_string.push_back(',');<br> temp_string.append(line);<br> temp_string.push_back('\n');<br> last_data.append(temp_string);<br> std::this_thread::sleep_for(std::chrono::seconds(1));<br> }<br><br> app_id.close();<br><br> std::ofstream file("data.txt");<br> if (file.is_open())<br> {<br> file << last_data;<br> file.close();<br> }<br> }<br><br> }<br><br>}<br>}.<br>```<br><br>The function `gen_data()` is generating random data points in real time from the `appid.txt` file and copying it to `data.txt` file. I want to run this function simultaneously alongside the main while loop so that the `data.txt` file is updates continuously in real time while the while loops runs.<br><br>Please guide me on how to implement multi threading in c++ so that I can run the function simultaneously.
Comments (19) 36246 👁️