multithreading - C++ program threading concepts -
i attempting write should simple c++ program. 3 parts:
- serial client: should poll serial server continuously , save received values in table, has infinite loop continue polling serial server
- logging: write current table values .csv file timestamp every few seconds
- 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
Post a Comment