void start(){ //std::thread mythread ( std::function<void()>(m_taskCreation) ); std::thread mythread ([&]()->void{ m_taskCreation.process();}); mythread.detach(); std::chrono::duration<int, std::centi> sleepTime ( 20); while ( m_taskCreation.isSupplyActive () ){ std::this_thread::sleep_for(sleepTime ); } m_done = true; }
int main(int argc, char **argv) { #ifdef __APPLE__ QPythonApp app(argc, argv); VCSSecondaryThread mythread(argc, argv); mythread.start(); return app.exec(); #else return Py_Main(argc, argv); #endif }
int main() { background_task bgtask; std::thread mythread(bgtask); std::vector<std::thread> myVector; for (int i = 0; i < 8; ++i) { // myVector.insert(bgtask); //TODO } // Инициализировали поток объектом-функцией // Продолжаем вычисления в главном потоке mythread.join(); return 0; }
void thwork(int m){ int j,k,res; struct struct_th *sth; pthread_t *thid; thid=(pthread_t*)malloc((m)*sizeof(pthread_t)); sth=(struct struct_th*)malloc((m)*sizeof(struct struct_th)); thid[0]=pthread_self(); for(k=0;k<m;k++){ sth[k].k=0; sth[k].j=0; sth[k].wl=k; } int ft; ft=time(NULL); for(k=1;k<m;k++){ res=pthread_create(&thid[k],NULL,&mythread,&sth[k]); if(res!=0){ printf("ERROR on thread creat, return value = %d\n",res); exit(-1); } } mythread(&sth[0]); for(k=1;k<m;k++){ pthread_join(thid[k],NULL); } ft=time(NULL)-ft; printf("time in seconds: %d\n",ft); free(sth); }
//organising nom_cln threads void thwork(int cln_num){ int j,k,res; key_t key, sem_key; struct STRUCT_TH *sth; pthread_t *thid; thid=(pthread_t*)malloc((cln_num)*sizeof(pthread_t)); sth=(struct STRUCT_TH*)malloc((cln_num)*sizeof(struct STRUCT_TH)); key = ftok(KEYFILE, 0); if(key < 0){ printf("ERROR in ftok!\n"); exit(-1); } msqid = msgget(key, 0666 | IPC_CREAT); if(msqid < 0){ printf("ERROR in msgget!\n"); exit(-1); } thid[0] = pthread_self(); for(k = 0; k < cln_num; k++){ sth[k].num_work_line = k; sth[k].cln_id=k+1; } for(k=1; k < cln_num; k++){ res=pthread_create(&thid[k], NULL, &mythread, &sth[k]); if(res != 0){ printf("ERROR on thread creat, return value = %d\n",res); exit(-1); } } mythread(&sth[0]); for(k = 1; k < cln_num; k++){ pthread_join(thid[k], NULL); } free(sth); free(thid); }
void CdbCheckDispatchResult_internal(struct CdbDispatcherState *ds, struct SegmentDatabaseDescriptor ***failedSegDB, int *numOfFailed, DispatchWaitMode waitMode) { int i; int j; int nFailed = 0; DispatchCommandParms *pParms; CdbDispatchResult *dispatchResult; SegmentDatabaseDescriptor *segdbDesc; Assert(ds != NULL); if (failedSegDB) *failedSegDB = NULL; if (numOfFailed) *numOfFailed = 0; /* * No-op if no work was dispatched since the last time we were called. */ if (!ds->dispatchThreads || ds->dispatchThreads->threadCount == 0) { elog(DEBUG5, "CheckDispatchResult: no threads active"); return; } /* * Wait for threads to finish. */ for (i = 0; i < ds->dispatchThreads->threadCount; i++) { pParms = &ds->dispatchThreads->dispatchCommandParmsAr[i]; Assert(pParms != NULL); /* * Does caller want to stop short? */ switch (waitMode) { case DISPATCH_WAIT_CANCEL: case DISPATCH_WAIT_FINISH: pParms->waitMode = waitMode; break; default: break; } if (gp_connections_per_thread == 0) { thread_DispatchWait(pParms); } else { elog(DEBUG4, "CheckDispatchResult: Joining to thread %d of %d", i + 1, ds->dispatchThreads->threadCount); if (pParms->thread_valid) { int pthread_err = 0; pthread_err = pthread_join(pParms->thread, NULL); if (pthread_err != 0) elog(FATAL, "CheckDispatchResult: pthread_join failed on thread %d (%lu) of %d (returned %d attempting to join to %lu)", i + 1, #ifndef _WIN32 (unsigned long) pParms->thread, #else (unsigned long) pParms->thread.p, #endif ds->dispatchThreads->threadCount, pthread_err, (unsigned long) mythread()); } } HOLD_INTERRUPTS(); pParms->thread_valid = false; MemSet(&pParms->thread, 0, sizeof(pParms->thread)); RESUME_INTERRUPTS(); /* * Examine the CdbDispatchResult objects containing the results * from this thread's QEs. */ for (j = 0; j < pParms->db_count; j++) { dispatchResult = pParms->dispatchResultPtrArray[j]; if (dispatchResult == NULL) { elog(LOG, "CheckDispatchResult: result object is NULL ? skipping."); continue; } if (dispatchResult->segdbDesc == NULL) { elog(LOG, "CheckDispatchResult: result object segment descriptor is NULL ? skipping."); continue; } segdbDesc = dispatchResult->segdbDesc; /* * segdbDesc error message is unlikely here, but check anyway. */ if (segdbDesc->errcode || segdbDesc->error_message.len) cdbdisp_mergeConnectionErrors(dispatchResult, segdbDesc); /* * Log the result */ if (DEBUG2 >= log_min_messages) cdbdisp_debugDispatchResult(dispatchResult, DEBUG2, DEBUG3); /* * Notify FTS to reconnect if connection lost or never connected. */ if (failedSegDB && PQstatus(segdbDesc->conn) == CONNECTION_BAD) { /* * Allocate storage. Caller should pfree() it. */ if (!*failedSegDB) *failedSegDB = palloc(sizeof(**failedSegDB) * (2 * getgpsegmentCount() + 1)); /* * Append to broken connection list. */ (*failedSegDB)[nFailed++] = segdbDesc; (*failedSegDB)[nFailed] = NULL; if (numOfFailed) *numOfFailed = nFailed; } /* * Zap our SegmentDatabaseDescriptor ptr because it may be * invalidated by the call to FtsHandleNetFailure() below. * Anything we need from there, we should get before this. */ dispatchResult->segdbDesc = NULL; } } /* * reset thread state (will be destroyed later on in finishCommand) */ ds->dispatchThreads->threadCount = 0; /* * It looks like everything went fine, make sure we don't miss a * user cancellation? * * The waitMode argument is NONE when we are doing "normal work". */ if (waitMode == DISPATCH_WAIT_NONE || waitMode == DISPATCH_WAIT_FINISH) CHECK_FOR_INTERRUPTS(); }