multithreading - C++ program threading concepts -


i attempting write should simple c++ program. 3 parts:

  1. serial client: should poll serial server continuously , save received values in table, has infinite loop continue polling serial server
  2. logging: write current table values .csv file timestamp every few seconds
  3. menu: simple command line menu start/stop server , exit program

i have tried use pthread , boost::thread make 3 functions occur simultaneously haven't had luck.

can provide me bit of direction on this, i'm new threading, , maybe threading isn't right way go here.

here way asking:

boost::mutex mtx;  void poll_thread() {     while(!done) {         poll_for_data();         if(data_received) {             boost::unique_lock<boost::mutex> lock(mtx);             //write data table         }     } }  void log_thread() {     while(!done) {         sleep(1);         boost::unique_lock<boost::mutex> lock(mtx);         //log table csv file...     } }  int main() {     //create , start polling , logging thread     boost::thread th1(&poll_thread);     boost::thread th2(&log_thread);      //run menu     while(!done) {         //...     }      th1.join();     th2.join();      return 0; } 

the mutex necessary avoid accessing table simultaneously in different threads.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -