void test1() { n_printf("\n Inside the first test now waiting for test2 to give latest value of y \n"); CondWait(cond); /*Waiting for the signal*/ y=y+20; n_printf("\n Test 1 is not waiting now and value of y is %d",y); }
int func2(void* arg) { int retval=0; ThreadData* pData = (ThreadData*) arg; retval = CondWait(pData); retval = VerifyResult(pData,EINVAL); StopThread(pData); return retval; }
int func(void* arg) { int retval=0; ThreadData* pData = (ThreadData*) arg; retval = MutexLock(pData); retval = CondWait(pData); retval = MutexUnlock(pData); StopThread(pData); return retval; }
// Function to read the table void readThread(int c) { // Transform the lock CondId cond = (int)c; // Wait our condition if (CondWait(cond) < 0) n_printf("Error condition wait"); int i; for (i = 0; i < NUM; i++) { n_printf("Tab[%d] = %d\n", i, tab[i]); } Exit(0); }
int main() { TThread loggingThread; TThread *loggerThreads = (TThread*)malloc(sizeof(TThread) * numThreadsToStart); unsigned int i = 0; printf("Creating threads\n"); /* fire off the consumer thread */ ThreadCreate(&loggingThread, log_error_thread, 0); sleep(1); /* fire off the producer threads */ for (i=0; i<numThreadsToStart; ++i) { int *thrNum = (int*)malloc(sizeof(int)); *thrNum = i; ThreadCreate(&loggerThreads[i], testLoggingThread, thrNum); } sleep(1); printf("Signalling START\n"); /* signal that the threads can start */ MutexLock(&cond_mutex); start_flag = 1; CondBroadcast(&start_cond); MutexUnlock(&cond_mutex); printf("Waiting for threads to complete\n"); MutexLock(&threadCountLock); while (threadCount) { CondWait(&threadCountCond, &threadCountLock); } MutexUnlock(&threadCountLock); sleep(10); printf("See ya later, alligator\n"); return EXIT_SUCCESS; }
static void *testLoggingThread(void *args) { unsigned int i = 0; char msg[256]; int *threadNum = (int*)(args); printf("Thread %d fired off\n", threadNum); MutexLock(&threadCountLock); ++threadCount; MutexUnlock(&threadCountLock); MutexLock(&cond_mutex); while (start_flag == 0) { CondWait(&start_cond, &cond_mutex); } MutexUnlock(&cond_mutex); printf("Thread %d actually going\n", *threadNum); for (i=0; i<numLogsPerThread; ++i) { int bytes = snprintf(msg, sizeof(msg), "Thread %d, operation %d: reporting for work, SIR!", *threadNum, i); append_log_message(msg, bytes); } printf("Thread %d finished logging\n", threadNum); MutexLock(&threadCountLock); --threadCount; CondSignal(&threadCountCond); MutexUnlock(&threadCountLock); printf("Thread %d exiting\n", threadNum); free(threadNum); return 0; }
static void BuildLoop(ThreadState* thread_state) { BuildQueue *queue = thread_state->m_Queue; ConditionVariable *cv = &queue->m_WorkAvailable; Mutex *mutex = &queue->m_Lock; MutexLock(mutex); while (ShouldKeepBuilding(queue, thread_state->m_ThreadIndex)) { if (NodeState* node = NextNode(queue)) { AdvanceNode(queue, thread_state, node, mutex); } else { CondWait(cv, mutex); } } MutexUnlock(mutex); Log(kSpam, "build thread %d exiting\n", thread_state->m_ThreadIndex); }
TInt CTestCondwait::TestCond409() { int errsum=0, err = 0; int retval = 0; ThreadData lThreadData; sem_t lSignalSemaphore; sem_t lSuspendSemaphore; sem_t lTestSemaphore; pthread_mutex_t lTestMutex; pthread_cond_t lTestCondVar; pthread_condattr_t lCondAttr; pthread_mutexattr_t lTestMutexAttr; pthread_mutexattr_t defaultattr; pthread_mutexattr_t errorcheckattr; pthread_mutexattr_t recursiveattr; pthread_mutexattr_init(&defaultattr); pthread_mutexattr_init(&errorcheckattr); pthread_mutexattr_init(&recursiveattr); pthread_mutexattr_settype(&errorcheckattr,PTHREAD_MUTEX_ERRORCHECK); pthread_mutexattr_settype(&recursiveattr,PTHREAD_MUTEX_RECURSIVE); pthread_mutex_t l_staticmutex = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t l_errorcheckmutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP; pthread_mutex_t l_recursivemutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; pthread_cond_t l_staticcondvar = PTHREAD_COND_INITIALIZER; CommonData lCommonData; lCommonData.iStaticMutex = &l_staticmutex; lCommonData.iErrorCheckMutex = &l_errorcheckmutex; lCommonData.iRecursiveMutex = &l_recursivemutex; lCommonData.iStaticCondVar = &l_staticcondvar; retval = sem_init(&lSignalSemaphore,0,0); if(retval != 0) { return retval; } retval = sem_init(&lSuspendSemaphore,0,0); if(retval != 0) { return retval; } lThreadData.iSignalSemaphore = &lSignalSemaphore; lThreadData.iSuspendSemaphore = &lSuspendSemaphore; lThreadData.iTestSemaphore = &lTestSemaphore; lThreadData.iTestMutex = &lTestMutex; lThreadData.iTestMutexAttr = &lTestMutexAttr; lThreadData.iTestCondVar = &lTestCondVar; lThreadData.iDefaultAttr = &defaultattr; lThreadData.iErrorcheckAttr = &errorcheckattr; lThreadData.iRecursiveAttr = &recursiveattr; lThreadData.iCondAttr = &lCondAttr; for (int loop = 0; loop < EThreadMain; loop++) { g_spinFlag[loop] = true; } lThreadData.iSuspending = false; lThreadData.iSpinCounter = 0; lThreadData.iCurrentCommand = -1; lThreadData.iSelf = EThreadMain; lThreadData.iValue = 0; lThreadData.iRetValue = 0; lThreadData.ierrno = 0; lThreadData.iExpectederrno = 0; lThreadData.iTimes = 0; lThreadData.iStopped = false; lThreadData.iCommonData = &lCommonData; retval = CondInit(&lThreadData); retval = CondWait(&lThreadData); retval = VerifyResult(&lThreadData,EINVAL); retval = CondDestroy(&lThreadData); StopThread(&lThreadData); err = pthread_cond_destroy(&l_staticcondvar); if(err != EINVAL) { errsum += err; } err = pthread_mutex_destroy(&l_recursivemutex); if(err != EINVAL) { errsum += err; } err = pthread_mutex_destroy(&l_errorcheckmutex); if(err != EINVAL) { errsum += err; } err = pthread_mutex_destroy(&l_staticmutex); if(err != EINVAL) { errsum += err; } err = pthread_mutexattr_destroy(&recursiveattr); if(err != EINVAL) { errsum += err; } err = pthread_mutexattr_destroy(&errorcheckattr); if(err != EINVAL) { errsum += err; } err = pthread_mutexattr_destroy(&defaultattr); if(err != EINVAL) { errsum += err; } err = sem_destroy(&lSignalSemaphore); if(err != EINVAL) { errsum += err; } err = sem_destroy(&lSuspendSemaphore); if(err != EINVAL) { errsum += err; } return retval+errsum; }
int CondHandleWait(cond_t cond) { if (cond < 0) return SYNC_FAIL; if (cond >= MAX_SEMS) return SYNC_FAIL; if (!conds[cond].inuse) return SYNC_FAIL; return CondWait(&conds[cond]); }