int TestConcurrentReadWrite(STORAGE::Filesystem *fs) { for (int i = 0; i < numWriters; ++i) { std::srand((unsigned int)std::time(NULL) + i); data[i] = random_string(dataSize); } THREADING::ThreadPool pool(numThreads); std::thread writeThread([&pool, fs] { for (int i = 0; i < numWriters; ++i) { pool.enqueue([fs, i] {startWriter(fs, i); }); } }); std::thread readThread([&pool, fs] { for (int i = 0; i < numReaders; ++i) { std::future<bool> ret = pool.enqueue([fs] {return startReader(fs); }); if (!ret.get()) { failure = true; break; } } }); readThread.join(); writeThread.join(); return failure; }
//******************************************************************* int CPRCommRS232::connect() { io_service io; const char *PORT = COMPORT; serial_port_base::baud_rate baud_option(460800); flag_connected_ = false; string s; try{ port_ = new serial_port(io); port_->open(PORT); // open the com port_ port_->set_option(baud_option); // set the baud rate flag_connected_ = true; std::cerr << "Port " << PORT << " opened\n"; s = "Port" + string(PORT) + "opened successfully"; }catch (boost::system::system_error &e){ // description of the failure boost::system::error_code ec = e.code(); s = "Could not open port_ " + string(PORT) + "! "; // + ec.category().name(); std::cerr << "Cannot open port_ " << PORT << " - error code: " << ec.category().name() << std::endl; }catch(std::exception e){ std::cerr << "Cannot open port_ " << PORT << " - error code: " << e.what() << endl; s = "Could not open port_ " + string(PORT) + "! "; // + string(e.what()); } boost::thread readThread(readLoop, (void*)this); // start the reading loop return true; }
inline bool PBFParser::Parse() { // Start the read and parse threads boost::thread readThread(boost::bind(&PBFParser::ReadData, this)); //Open several parse threads that are synchronized before call to boost::thread parseThread(boost::bind(&PBFParser::ParseData, this)); // Wait for the threads to finish readThread.join(); parseThread.join(); return true; }
void Process::launch(int nbrThread) { this->_threadPool = new ThreadPool<ProcessData>(nbrThread); std::thread readThread(&Process::readInfo, this); std::thread writeThread(&Process::writeInfo, this); readThread.join(); this->_mutex.lock(); this->_finished = true; this->_mutex.unlock(); writeThread.join(); delete this->_namedPipeWrite; delete this->_namedPipeRead; delete this->_threadPool; _Exit(0); }
inline void PBFParser::ParseStep(ParsingStep step) { parsingStep = step; ReadHeader(); #ifndef NDEBUG blockCount = 0; groupCount = 0; #endif // Start the read and parse threads boost::thread readThread(boost::bind(&PBFParser::ReadData, this)); //Open several parse threads that are synchronized before call to boost::thread parseThread(boost::bind(&PBFParser::ParseData, this)); // Wait for the threads to finish readThread.join(); parseThread.join(); CleanQueue(); }
int main( int argc, char **argv ) { TEST(( PACKETSIZE % 8 ) == 0 ); co::init( argc, argv ); for( size_t i = 0; types[i] != co::CONNECTIONTYPE_NONE; ++i ) { co::ConnectionDescriptionPtr desc = new co::ConnectionDescription; desc->type = types[i]; co::ConnectionPtr writer; co::ConnectionPtr listener; if( !_initialize( desc, listener, writer )) continue; Reader readThread( listener ); if( desc->type != co::CONNECTIONTYPE_RSP ) TEST( writer->connect( )); uint64_t out[ PACKETSIZE / 8 ]; lunchbox::Clock clock; uint64_t sequence = 0; while( clock.getTime64() < RUNTIME ) { out[0] = ++sequence; TEST( writer->send( out, PACKETSIZE )); } out[0] = 0xdeadbeef; TEST( writer->send( out, PACKETSIZE )); s_done.waitEQ( true ); writer->close(); readThread.join(); listener->close(); const float bwTime = clock.getTimef(); const uint64_t numBW = sequence; TEST( _initialize( desc, listener, writer )); Latency latency( listener ); if( desc->type != co::CONNECTIONTYPE_RSP ) TEST( writer->connect( )); sequence = 0; clock.reset(); while( clock.getTime64() < RUNTIME ) { ++sequence; TEST( writer->send( &sequence, sizeof( uint64_t ))); } sequence = 0xC0FFEE; TEST( writer->send( &sequence, sizeof( uint64_t ))); s_done.waitEQ( true ); writer->close(); latency.join(); listener->close(); const float latencyTime = clock.getTimef(); const float mFactor = 1024.f / 1024.f * 1000.f; std::cout << desc->type << ": " << (numBW+1) * PACKETSIZE / mFactor / bwTime << " MBps, " << (sequence+1) / mFactor / latencyTime << " Mpps" << std::endl; if( listener == writer ) listener = 0; TESTINFO( !listener || listener->getRefCount() == 1, listener->getRefCount()); TEST( writer->getRefCount() == 1 ); } co::exit(); return EXIT_SUCCESS; }
int main() { // Setup ROS Arguments char** argv = NULL; int argc = 0; // Init ROS Node ros::init (argc, argv, "MR_Button"); ros::NodeHandle nh; ros::NodeHandle pNh ("~"); // Topic names std::string mrMainRunSrv, buttonSub, obstacleDetectorSub, mrMainModeSub; pNh.param<std::string> ("mr_button_sub", buttonSub, "/mrButton/status"); pNh.param<std::string> ("mr_obstacle_detector", obstacleDetectorSub, "/mrObstacleDetector/status"); pNh.param<std::string> ("mr_main_mode_sub", mrMainModeSub, "/mrMain/mode"); pNh.param<std::string> ("mr_main_run_srv", mrMainRunSrv, "/mrMain/run"); // Service _srvMrMainRun = nh.serviceClient<mr_main::run>(mrMainRunSrv); // Subscriber ros::Subscriber subButton = nh.subscribe (buttonSub, 1, buttonCallback); ros::Subscriber subObstacleDetector = nh.subscribe (obstacleDetectorSub, 1, obstacleDetectorCallback); ros::Subscriber subMrMainMode = nh.subscribe (mrMainModeSub, 1, mrMainModeCallback); // Get serial data parameters int baudRate; std::string port; pNh.param<int> ("baud_rate", baudRate, 115200); pNh.param<std::string> ("port", port, "/dev/serial/by-id/usb-Texas_Instruments_In-Circuit_Debug_Interface_0E203B83-if00"); // Open connection _serialConnection = new serial::Serial (port.c_str(), baudRate, serial::Timeout::simpleTimeout (50)); // Check if connection is ok if (!_serialConnection->isOpen()) { ROS_ERROR ("Error opening connection!"); _serialConnection->close(); return 0; } else ROS_INFO ("Successfully connected!"); // Start serial threads boost::thread readThread(readSerialThread); boost::thread writeThread(writeSerialThread); // Sleep for a second ros::Duration(2).sleep(); // Change mode to idle changeRunMode (M_IDLE); // ROS Spin: Handle callbacks while (!ros::isShuttingDown()) ros::spin(); // Close connection changeRunMode (M_OFF); ros::Duration(2).sleep(); readThread.interrupt(); writeThread.interrupt(); _serialConnection->close(); // Return return 0; }