void sumCMatrixElements(matrix_data_t *m) { int i, j; for ( i = 0; i < m->C.rows; i++ ) { int ret; int thID = getFreeThread(); thread_data_t *threadData = malloc(sizeof(thread_data_t)); threadData->i = i; threadData->threadID = thID; threadData->m = m; if(activeThreads[thID] == 2) //juz raz uzylismy... { /* VERBOSE fprintf(stderr, "Reusing thread... securing with pthread_join just in case\n"); */ int *val; ret = pthread_join(threads[thID], (void **)&val); fprintf(stderr, "Thread %d exit status %d\n", thID, val); if( ret != 0) { fprintf(stderr, "%s:%d pthread_join fail %d\n", __FILE__, __LINE__, ret); exit(EXIT_FAILURE); } else { /* VERBOSE fprintf(stderr, "%s:%d pthread_join OK\n", __FILE__, __LINE__); */ activeThreads[thID] = 1; } } setThreadState(thID, 0);// Busy //raise(SIGINT); /* VERBOSE fprintf(stderr, "Thread %d\n", thID); */ ret = pthread_create( &threads[thID], NULL, sumThread, (void*) threadData); if( ret != 0) { fprintf(stderr, "%s:%d pthread_create fail %d\n", __FILE__, __LINE__, ret); exit(EXIT_FAILURE); } } for (i = 0; i < MAX_THREADS;i++) { if(getThreadState(i) == 2 || getThreadState(i) == 0) { int *val; int ret = pthread_join(threads[i], (void **)&val); fprintf(stderr, "Thread %d exit status %d\n", i, val); if( ret != 0) { fprintf(stderr, "%s:%d pthread_join fail %d\n", __FILE__, __LINE__, ret); exit(EXIT_FAILURE); } else { /* VERBOSE fprintf(stderr, "%s:%d pthread_join OK\n", __FILE__, __LINE__); */ activeThreads[i] = 1; } } } }
/** * 別スレッドで指定した関数を実行します。 */ ThreadPool::ThreadHandle ThreadPool::run(FuncPtr func, void *arg) { ThreadHandle index = getFreeThread(); if(index == INVALID_THREAD_HANDLE){ return INVALID_THREAD_HANDLE; } threads_[index]->run(func, arg); return index; }
void PingScheduler::processPingResult(QString ipAddress, int result, bool monitoring) { QTextStream cout(stdout); if (monitoring) { if (result == 0) { // reschedule according to up schedule ObjectInstances2::scheduler.addHostToSchedule(ipAddress, true); } else { // reschedule according to down schedule ObjectInstances2::scheduler.addHostToSchedule(ipAddress, false); } cout << "PINGRESULT:" << ipAddress << ";" << result << monitoring << endl; ObjectInstances2::processController.messageAnalyzer("PINGRESULT:" + ipAddress + ";" + QString::number(result)); } else { QTextStream cout(stdout); cout << "PINGRESULT:" << ipAddress << ";" << result << endl; //emit sendPingResult(ipAddress, result); } int free_thread = getFreeThread(); if (free_thread >= 0) { if (!monitor_queue.isEmpty()) { QString ip_address = monitor_queue.dequeue(); pingthreadcontrols[free_thread].startPing(ip_address, true); } else { if (!discover_queue.isEmpty()) { QString ip_address = discover_queue.dequeue(); pingthreadcontrols[free_thread].startPing(ip_address, false); } } } }
void PingScheduler::schedulePing(QString ip_address, bool monitoring) { int freeThread = getFreeThread(); if (freeThread >= 0) { pingthreadcontrols[freeThread].startPing(ip_address, monitoring); } else { if (monitoring) { monitor_queue.enqueue(ip_address); } else { discover_queue.enqueue(ip_address); } } }
int CAdvThreadPool::startPoolMainFunction() { systemThreadRunnableSign = true; bool isAdditionalThreadsQuantityChanged = false; int timeout; infoTimer.start(); while(systemThreadRunnableSign) { QThread::msleep(10); //check repeated tasks std::unique_lock<std::mutex> locker(repeatTaskMutex); for(auto it = repeatedTaskArray.begin(); it != repeatedTaskArray.end(); it++) { timeout = it->qt_timer.elapsed(); if(timeout >= it->timePeriod) { int threadId = -1; QString runType = it->run(int(CAdvThread::eRUN_MODES::RUN_TYPE), 0); adv_thread_ptr pThread; pThread = getFreeThread(threadId, runType.toInt()); if(pThread == nullptr) { emitter->sendSignal_NoThread(); continue; } pThread->appendRunnableTask(it->run, runType.toInt()); it->qt_timer.start(); } } //notification about suspicious behavior auto currentTime = std::chrono::high_resolution_clock::now(); for (auto &it : threadsArray) { CAdvThread::eThreadType l_iThreadType = it->getThreadType(); if(l_iThreadType == CAdvThread::eThreadType::THREAD_SHARED) //SHORT_TASK || REPEAT_TASK { if (!(it->isEmpty())) { auto lastTimeOfTaskLaunch = it->getLastTimeOfTaskLaunch(); auto deltaTime = std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - lastTimeOfTaskLaunch).count(); if((it->isReadyForSend_WarningAboutShortTaskFreeze) && deltaTime > 5000)//very suspicious { getEmitter()->addWarningToShell(QString("Maybe task '%1' was frozen (very suspicious)").arg(it->getWho()), eLogWarning::warning); it->isReadyForSend_WarningAboutShortTaskFreeze = false; } } } } //delete extra long threads which was stoped additionalThreadsMutex.lock(); isAdditionalThreadsQuantityChanged = false; additionalThreadsArray.remove_if( [&isAdditionalThreadsQuantityChanged](adv_thread_ptr thr) { if(thr->isStoped()) { qDebug() << "Remove thread from additionalThreadsArray"; isAdditionalThreadsQuantityChanged = true; return true; } else return false; }); additionalThreadsMutex.unlock(); if(isAdditionalThreadsQuantityChanged) { int longTaskCounter = CAdvThreadPool::getInstance().getLongTaskQuantity(); emitter->sendSignal_longTaskQuantity(longTaskCounter); } } return 0; }