void MainWindow::beginWork() { addFormattedMessage("Starting worker thread:\n\tGameDir = \"%s\"\n\tOutputFile = \"%s\"\n\tSAMPObjects = %s\n\tCustomObjects = %s\n\n", gamePath.toLocal8Bit().data(), outputPath.toLocal8Bit().data(), (ui_settings->sampObjectsBox->isChecked() ? "true" : "false"), (ui_settings->customObjectsBox->isChecked() ? "true" : "false")); updateThread(); threadTimer->start(); workerThread = new std::thread(doWork, gamePath.toStdString(), outputPath.toStdString(), ui_settings->sampObjectsBox->isChecked(), ui_settings->customObjectsBox->isChecked()); }
void Master::start() { thread updateThread(&Master::updateMappingToNodes, this); thread healthCheckThread(&Master::healthCheck, this); grpc::ServerBuilder builder; builder.AddListeningPort(_address, grpc::InsecureServerCredentials()); builder.RegisterService(this); std::unique_ptr<grpc::Server> server(builder.BuildAndStart()); server->Wait(); updateThread.join(); healthCheckThread.join(); }
void MessageWidget::addSMS(QString threadId, QString messageId, QString timestamp, QString number, QString read, QString toa, QString body) { QString contactName = this->contactModel.getName(number); this->messageCount++; if (!this->messageThreadModel.exists(threadId)) { this->messageCount = 1; this->messageThreadModel.addThread(MessageThread(threadId,number,timestamp,read,body,QString::number(this->messageCount),contactName)); } else { updateThread(threadId, timestamp ,body, QString::number(this->messageCount), read); } this->messageModel.addMessage(Message(threadId, messageId, timestamp, number, read, toa, body, contactName)); }
void ThreadsHandler::updateThreads(const GdbMi &data) { // ^done,threads=[{id="1",target-id="Thread 0xb7fdc710 (LWP 4264)", // frame={level="0",addr="0x080530bf",func="testQString",args=[], // file="/.../app.cpp",fullname="/../app.cpp",line="1175"}, // state="stopped",core="0"}],current-thread-id="1" // Emit changed for previous frame. if (m_currentIndex != -1) { dataChanged(m_currentIndex); m_currentIndex = -1; } ThreadId currentId; const GdbMi current = data["current-thread-id"]; if (current.isValid()) currentId = ThreadId(current.data().toLongLong()); const QList<GdbMi> items = data["threads"].children(); const int n = items.size(); for (int index = 0; index != n; ++index) { const GdbMi item = items.at(index); const GdbMi frame = item["frame"]; ThreadData thread; thread.id = ThreadId(item["id"].toInt()); thread.targetId = item["target-id"].toLatin1(); thread.details = item["details"].toLatin1(); thread.core = item["core"].toLatin1(); thread.state = item["state"].toLatin1(); thread.address = frame["addr"].toAddress(); thread.function = frame["func"].toLatin1(); thread.fileName = frame["fullname"].toLatin1(); thread.lineNumber = frame["line"].toInt(); thread.module = QString::fromLocal8Bit(frame["from"].data()); thread.stopped = true; thread.name = item["name"].toLatin1(); if (thread.state == QLatin1String("running")) thread.stopped = false; if (thread.id == currentId) m_currentIndex = index; updateThread(thread); } if (m_currentIndex != -1) dataChanged(m_currentIndex); updateThreadBox(); }
void Game::run() { if (!initialized) { return; } instance = newInstance; newInstance = nullptr; Assets::clean(); Thread updateThread([]() { lastUpdate = Time::get(); while (!metallicar::quit && instance) { bool updated = false; updateTimeElapsed(); while (timeElapsedGreaterThanStep()) { updated = true; renderingBackBuffer->clear(); Input::pollEvents(); instance->update(); instance->render(); if (newInstance) { // change instance delete instance; instance = newInstance; newInstance = nullptr; Assets::clean(); } } if (updated) { // swap rendering buffers renderingBuffersLock.mutexlock(); map<double, list<function<void()>>>* tmp = renderingFrontBuffer; renderingFrontBuffer = renderingBackBuffer; renderingBackBuffer = tmp; renderingBuffersLock.unlock(); } Thread::sleep(1); } }); updateThread.start(); Thread audioCleanupThread([]() { while (!metallicar::quit && instance) { Audio::clean(); Thread::sleep(500); } }); audioCleanupThread.start(); // main thread (I/O) while (!metallicar::quit && instance) { updateFPS(); Input::pollWindowEvents(); // copy front buffer renderingBuffersLock.mutexlock(); map<double, list<function<void()>>> renderers(*renderingFrontBuffer); renderingBuffersLock.unlock(); // render Graphics::prepareFrame(); for (auto& kv : renderers) { for (auto& renderer : kv.second) { renderer(); } } Graphics::finalizeFrame(); Window::update(); } updateThread.join(); audioCleanupThread.join(); }
void n2dEngineImpl::MultiThreadMainLoop(ncTStr title, nuInt FPS) { UpdateThread updateThread(this, FPS); RenderThread renderThread(this, FPS); if (!m_pListener->EngineInit()) { TerminateApplication(); } while (m_IsProgramLooping.load(std::memory_order_acquire)) { if (m_Window.Create(title, m_ClassName, m_hInstance, this)) { MSG msg; m_Window.Show(); CommonInit(); if (!m_pListener->WindowInit()) { TerminateApplication(); } else { wglMakeCurrent(NULL, NULL); updateThread.Resume(); renderThread.Resume(); bool isMessagePumpActive = true; m_Keys.Clear(); while (isMessagePumpActive) { if (PeekMessage(&msg, m_Window.GetWnd(), 0, 0, PM_REMOVE)) { if (msg.message != WM_QUIT) { DispatchMessage(&msg); } else { isMessagePumpActive = false; } } if (!m_IsVisible) { WaitMessage(); } } if (updateThread.Wait(10u)) if (renderThread.Wait(10u)) break; if (PeekMessage(&msg, m_Window.GetWnd(), 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } } m_pListener->WindowUninit(); } else { m_IsProgramLooping.store(false, std::memory_order_release); nat_Throw(natException, _T("Failed to create GL window.")); } } updateThread.Wait(); renderThread.Wait(); m_pListener->EngineUninit(); }
DWORD ServiceExecutionThread(LPDWORD param) { #if _DEBUG ::OutputDebugString(L"ServiceExecutionThread\n"); #endif if (IntializeSystemInfo() == FALSE) { return IC_NO_INITIALIZE; } WORD wVersionRequested = MAKEWORD(1, 1); WSADATA wsaData; WSAStartup(wVersionRequested, &wsaData); SOCKET udpSocket = INVALID_SOCKET; int result = IC_NOERROR; bool showHelp = false; do { g_killServiceEvent = CreateEvent(0, TRUE, FALSE, 0); if (!g_killServiceEvent) { OutputError(L"CreateEvent fails! (%d)", GetLastError()); result = IC_EVENT_CREATE_FAIL; break; } wstring apiKey; wstring envInfo; vector<int> intervalTimes; ConnectionInfo connection; g_debugMode = GetDebugMode(g_argc, g_argv); if (cmdOptionExists(g_argv, g_argv + g_argc, L"-h") == true || cmdOptionExists(g_argv, g_argv + g_argc, L"/h") == true) { showHelp = true; break; } if (cmdOptionExists(g_argv, g_argv + g_argc, L"-unreg") == true) { DoUnregistration(); break; } if (cmdOptionExists(g_argv, g_argv + g_argc, L"-start") == true) { DoStartService(); break; } if (cmdOptionExists(g_argv, g_argv + g_argc, L"-stop") == true) { DoStopService(); break; } if (cmdOptionExists(g_argv, g_argv + g_argc, L"-update") == true) { ProcessLatestUpdate(); break; } if (cmdOptionExists(g_argv, g_argv + g_argc, L"-restart") == true) { RestartService(); break; } apiKey = GetApiKey(g_argc, g_argv); envInfo = GetEnvInfo(g_argc, g_argv); intervalTimes = GetIntervalTime(g_argc, g_argv); if (apiKey.length() == 0) { OutputError(L"NO ApiKey\n"); result = IC_NO_APIKEY; showHelp = true; break; } if (envInfo.length() == 0) { OutputError(L"NO AgentID Info\n"); result = IC_NO_AGENTIDINFO; showHelp = true; break; } string address = GetHostAddress(g_argc, g_argv, connection); struct hostent *host = gethostbyname(address.c_str()); if (host == nullptr) { OutputError(L"Can't resolve host address: %s\n", address.c_str()); result = IC_NO_RESOLVE_HOSTADDR; break; } SetupHostPort(g_argc, g_argv, connection); if (cmdOptionExists(g_argv, g_argv + g_argc, L"-regservice") == true) { DoRegistration(apiKey, envInfo, address, connection.Getport(), intervalTimes); break; } struct sockaddr_in remoteServAddr; remoteServAddr.sin_family = host->h_addrtype; memcpy((char *)&remoteServAddr.sin_addr.s_addr, host->h_addr_list[0], host->h_length); remoteServAddr.sin_port = htons((u_short)connection.Getport()); /* socket creation */ udpSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (udpSocket == INVALID_SOCKET) { OutputError(L"socket failed with error: %ld\n", WSAGetLastError()); break; } wstring appVersion = GetAppVersion(g_moduleFilePath.c_str(), NULL, NULL, NULL, NULL); OutputConsole(L"(%s) ServiceExecutionThread - data collect thread - start\n", appVersion.c_str()); { thread processCpuMemThread([apiKey, envInfo, udpSocket, remoteServAddr, intervalTimes]() { ProcessCpuMemInfo(apiKey, envInfo, udpSocket, remoteServAddr, intervalTimes[0]); }); thread processDiskThread([apiKey, envInfo, udpSocket, remoteServAddr, intervalTimes]() { ProcessDiskInfo(apiKey, envInfo, udpSocket, remoteServAddr, intervalTimes[1]); }); thread updateThread([]() { DWORD oneday = 1000 * 60 * 60 * 24; while (true) { ProcessLatestUpdate(); if (::WaitForSingleObject(g_killServiceEvent, oneday) == WAIT_TIMEOUT) { continue; } break; } }); if (g_isConsoleApp == TRUE) { printf("Press any key to exit...\n"); getchar(); } else { #if _DEBUG ::OutputDebugString(L"Service thread - WaitForSingleObject...\n"); #endif WaitForSingleObject(g_killServiceEvent, INFINITE); } #if _DEBUG ::OutputDebugString(L"Service thread waiting...\n"); Sleep(1000); #endif if (processCpuMemThread.joinable() == true) { processCpuMemThread.join(); } if (processDiskThread.joinable() == true) { processDiskThread.join(); } if (updateThread.joinable() == true) { updateThread.join(); } #if _DEBUG ::OutputDebugString(L"All Service-threads exited...\n"); #endif } ::SetEvent(g_killSafeExitEvent); #if _DEBUG ::OutputDebugString(L"Service thread detached\n"); #endif } while (false); #if _DEBUG ::OutputDebugString(L"ServiceExecutionThread - data collect thread - ending\n"); #endif if (udpSocket != INVALID_SOCKET) { closesocket(udpSocket); } if (showHelp == true) { ShowHelp(); } WSACleanup(); #if _DEBUG ::OutputDebugString(L"ServiceExecutionThread - data collect thread - ended\n"); #endif return result; }
void Robot::OperatorControl() //teleop code { CameraServer::GetInstance()->SetQuality(50); CameraServer::GetInstance()->StartAutomaticCapture("cam0"); bool testThreadRun = true; bool updateThreadRun = true; std::thread testThread(threadTestFunction, &testThreadRun); std::thread updateThread(updatePositionFunction, &updateThreadRun, &driveStick, &position); float throttle; float moveValue; float rotateValue; float shooterSpeed; float shooterAngle; shooter.Enable(); driveTrain.Enable(); while(IsOperatorControl() && IsEnabled()) { throttle = (((-driveStick.GetRawAxis(Constants::driveL2)) + 1.0)/2.0); //[0, 1] moveValue = throttle * driveStick.GetY(); rotateValue = -driveStick.GetX(); shooterSpeed = -driveStick.GetRawAxis(Constants::driveRightStickY); //change shooterAngle = 0; //change SmartDashboard::PutNumber("Throttle Value", throttle); SmartDashboard::PutNumber("Move Value", moveValue); SmartDashboard::PutNumber("Rotate Value", rotateValue); float shooterSpeed = -driveStick.GetRawAxis(Constants::driveRightStickY); if (shooterSpeed < -0.35) { shooterSpeed = -0.35; } driveTrain.ArcadeDrive(moveValue, rotateValue, true); shooter.SetSpeed(shooterSpeed, -shooterSpeed); if (driveStick.GetRawButton(Constants::calibrateButton)) { position.Calibrate(); } if (driveStick.GetRawButton(Constants::shootButton)) { if (!shooter.HasBall()) { shooter.LoadBall(); } if (shooter.HasBall()) { shooter.PrepareShooter(shooterAngle, shooterSpeed); //TODO: Change these. Get physics major's equation } Wait(1); //how long to wait to shoot. Will need to be adjusted with testing if (shooter.Angle() > shooterAngle - 2 && shooter.Angle() < shooterAngle + 2 && shooter.WheelSpeed() == shooterSpeed) { shooter.Shoot(); } } SmartDashboard::PutNumber("xPos", position.GetX()); SmartDashboard::PutNumber("yPos", position.GetY()); SmartDashboard::PutString("Version", "Noah 2/14/16, 11:16 PM"); } shooter.Disable(); driveTrain.Disable(); testThreadRun = false; testThread.join(); updateThreadRun = false; updateThread.join(); driveTrain.SetSafetyEnabled(true); }