title: How can I create a persistent list of globally accesible objects queried from threads?
body: <p>I've been trying to get a persistent object from a thread for hours.
  I want to write a shared library in C++ that starts a persistent loop in a function.
  In the following code snippets there is a class called Process. Process initializes a TCP/IP interface to read and write data from a Simulink model.</p>
  
  <p>This is only for declaration and should not be important for this problem, but now you know what I talk about when mentioning the processes.</p>
  
  <h3>main.cpp</h3>
  
  <p>I know, it looks kinda ugly/unprofessional, but I'm fairly new to C++..</p>
  
  <pre class="lang-cpp prettyprint-override"><code>// frustrated attempt to make everything persistent
  static vector&lt;std::thread&gt; processThreads;
  static ProcessHandle processHandle;
  static vector&lt;std::promise&lt;Process&gt;&gt; promiseProcess;
  static vector&lt;std::future&lt;Process&gt;&gt; futureProcess;
  
  EXPORT int initializeProcessLoop(const char *host, int port)
  {
      std::promise&lt;Process&gt; promiseObj;
      futureProcess.push_back(std::future&lt;Process&gt;(promiseObj.get_future()));
      processThreads.push_back(std::thread(&amp;ProcessHandle::addProcess, processHandle, host, port, &amp;promiseProcess[0]));
  
      Process val = futureProcess[0].get();
  
      processHandle.handleList.push_back(val);
  
      return (processHandle.handleList.size() - 1);
  }
  </code></pre>
  
  <h3>ProcessHandle.cpp</h3>
  
  <p>The addProcess function from ProcessHandle creates the Process that should be persistent, adds it to a static vector member of ProcessHandle and passes the promise to the execution loop.</p>
  
  <pre class="lang-cpp prettyprint-override"><code>int ProcessHandle::addProcess(const char *address, int port, std::promise&lt;Process&gt; * promiseObj) {
      Process process(address, port);
  
      handleList.push_back(process);
      handleList[handleList.size() - 1].exec(promiseObj);
      return handleList.size() - 1;
  }
  </code></pre>
  
  <p>To the main problem now...</p>
  
  <p>If I change "initializeProcessLoop" to include:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>    if(processHandle.handleList[0].isConnected())
      {
          processHandle.handleList[0].poll("/Compare To Constant/const");
      }
  </code></pre>
  
  <p>after i've pushed "val" to the processHandle.handleList everything works fine and I can poll the data as it should be.</p>
  
  <p>If I instead poll it from - for examle - the main function, the loop crashes inside of the "initializeProcessLoop" because "Process val" is reassigned (?) with futureProcess[0].get().</p>
  
  <p>How can I get the Process variable and the threaded loop to be consistent after the function returns?</p>
  
  <p>If there are any questions to the code (and I bet there will be), feel free to ask. Thanks in advance!</p>
  
  <p>PS: Obligatory "English is not my native language, please excuse any spelling errors or gibberish"...</p>