title: Multithreading background thread goes inactive
body: <p>First of all, I am a beginner on multithreading, so sorry for basic mistakes.</p>
  
  <p>I stream data from 5 sensors via TCP packages. Each of the sensors initializes and sends data in the background on a thread.</p>
  
  <p>The suppliers SDK is programmed in that way: it queues packages in a buffer and unloads them through a socket. If the CPU is busy and the buffer gets too large (capped at 100 packets), it dropps them and waits for the CPU to unload.</p>
  
  <p>Looking at Sensor 4, this is the bahaviour, that is observed, when operating multiple threads/sensors at the same time:</p>
  
  <pre><code>Sensor 0: THIS THREAD IS ACTIVE: 0 ID: 120600400447232 on CPU: 0 
  Sensor 0: buff Queue size: 1 
  Sensor 0: time for unloading buffer: 0ms 
  Sensor 2: THIS THREAD IS ACTIVE: 2 ID: 120600400654848 on CPU: 2 
  Sensor 2: buff Queue size: 1 
  Sensor 2: time for unloading buffer: 0ms 
  Sensor 4: buff Queue size: 101 
  Sensor 4: Warning: Client dropped packet due to full buffer... 
  Sensor 0: THIS THREAD IS ACTIVE: 0 ID: 120600400447232 on CPU: 0 
  Sensor 0: buff Queue size: 1 
  Sensor 0: time for unloading buffer: 0ms 
  Sensor 2: THIS THREAD IS ACTIVE: 2 ID: 120600400654848 on CPU: 2 
  Sensor 2: buff Queue size: 1 
  Sensor 2: time for unloading buffer: 0ms
  </code></pre>
  
  <p>This output repeats over and over again, the thread of sensor 4 is inactive for a couple of seconds (I also implemented timestamps at some points).</p>
  
  <p>Of course the Output is not thread-safe but should give a brief overview on what is happening. I assigned sensor 0 to cpu 0, sensor 1 to cpu 1, etc... The programm switches between Thread 0 and 2, while thread 4 is at full buffer. This appears after a short amount of time with 2-5 sensors active. Only one works just fine.</p>
  
  <p>Telling thread 0 and 2 to sleep at this point does not solve the issue. Also this is my lscpu: </p>
  
  <p>x86_64<br>
  CPUs: 12<br>
  Threads per core: 2<br>
  Cores per socket: 6<br>
  Sockets: 1<br></p>
  
  <p>Why is it, that only 2 threads are active at the same time? Is there a way to tell specific threads to "go active"? Or is there a general issue on how I structured the threads?</p>
  
  <p>Thank you and regards</p>
  
  <p>Alex</p>
  
  <pre><code>// main:
  struct *DataArrayGlobal0 {...}; 
  struct *DataArrayGlobal1 {...}; 
  // ... 5 times
  
  sensor0 = new sensor("192.168.10.200"); 
  sensor1 = new sensor("192.168.10.201"); 
  // ... 5 times
  
  sensor0-&gt;DataArrayGlobal = DataArrayGlobal0;
  sensor1-&gt;DataArrayGlobal = DataArrayGlobal1;
  // ... 5 times
  
  sensor0-&gt;init(); 
  sensor1-&gt;init(); 
  // ... 5 times
  
  // Do some other run methods
  // To process they copy the data into their own array
  // The threads keep running and are not joined yet
  
  sensor0-&gt;deInit();
  // .... 5 times
  // join threads and clean up
  
  
  class sensor {
  private:
      std::thread start_thread;
      struct myDataArray {...};
      std::mutex mutex;
      // some methods
  public:
      struct *DataArrayGlobal;
      void init();
      void start_sensor();
      // some methods
  }
  
  void sensor::init() 
  { 
      // some init stuff
      start_thread = thread(&amp;start_sensor, this); // this is an infinite loop until invokeShutdown
  }
  
  void sensor::start_sensor() 
  { 
      // do some stuff to create data array myDataArray 
      std::lock_guard guard(mutex); 
      std::copy(myDataArray-&gt;begin(), myDataArray-&gt;end(), DataArrayGlobal); 
  }
  
  void sensor::invokeShutdown()
  {
      start_thread.join();
  }
  
  void sensor::deInit()
  {
      invokeShutdown();
  }
  </code></pre>