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; }
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); }
void iterateThreads(sqlite::connection& con, const QDir& output_dir) { std::vector<std::string> threads; sqlite::query threadQuery(con, "SELECT DISTINCT thread FROM messages"); sqlite::result_type threadResults = threadQuery.get_result(); while(threadResults->next_row()) { std::string a_thread = threadResults->get_string(0); threads.push_back(a_thread); } sqlite::query gmtQuery(con, "SELECT `value` FROM meta WHERE `key` = 'gmt_offset'"); sqlite::result_type gmtResult = gmtQuery.get_result(); gmtResult->next_row(); int gmt_offset = stoi(gmtResult->get_string(0)); for(std::string a_thread : threads) { writeThread(output_dir, a_thread, gmt_offset, con); } }
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; }
void WSMSGridder::predictMeasurementSet(MSData &msData) { msData.msProvider->ReopenRW(); const MultiBandData selectedBandData(msData.SelectedBand()); _gridder->PrepareBand(selectedBandData); size_t rowsProcessed = 0; ao::lane<PredictionWorkItem> calcLane(_laneBufferSize+_cpuCount), writeLane(_laneBufferSize); lane_write_buffer<PredictionWorkItem> bufferedCalcLane(&calcLane, _laneBufferSize); boost::thread writeThread(&WSMSGridder::predictWriteThread, this, &writeLane, &msData); boost::thread_group calcThreads; for(size_t i=0; i!=_cpuCount; ++i) calcThreads.add_thread(new boost::thread(&WSMSGridder::predictCalcThread, this, &calcLane, &writeLane)); /* Start by reading the u,v,ws in, so we don't need IO access * from this thread during further processing */ std::vector<double> us, vs, ws; std::vector<size_t> rowIds, dataIds; msData.msProvider->Reset(); while(msData.msProvider->CurrentRowAvailable()) { size_t dataDescId; double uInMeters, vInMeters, wInMeters; msData.msProvider->ReadMeta(uInMeters, vInMeters, wInMeters, dataDescId); const BandData& curBand(selectedBandData[dataDescId]); const double w1 = wInMeters / curBand.LongestWavelength(), w2 = wInMeters / curBand.SmallestWavelength(); if(_gridder->IsInLayerRange(w1, w2)) { us.push_back(uInMeters); vs.push_back(vInMeters); ws.push_back(wInMeters); dataIds.push_back(dataDescId); rowIds.push_back(msData.msProvider->RowId()); ++rowsProcessed; } msData.msProvider->NextRow(); } for(size_t i=0; i!=us.size(); ++i) { PredictionWorkItem newItem; newItem.u = us[i]; newItem.v = vs[i]; newItem.w = ws[i]; newItem.dataDescId = dataIds[i]; newItem.data = new std::complex<float>[selectedBandData[dataIds[i]].ChannelCount()]; newItem.rowId = rowIds[i]; bufferedCalcLane.write(newItem); } if(Verbose()) std::cout << "Rows that were required: " << rowsProcessed << '/' << msData.matchingRows << '\n'; msData.totalRowsProcessed += rowsProcessed; bufferedCalcLane.write_end(); calcThreads.join_all(); writeLane.write_end(); writeThread.join(); }