Ejemplo n.º 1
0
/*virtual*/
void_t
Test_Condition::unit(
    culonglong_t &a_caseLoops
)
{
    xUNUSED(a_caseLoops);

#if xENV_UNIX && xTODO
    int_t          iRv          = - 1;
    pthread_t    thThreads[3] = {0};
    clong_t liId1        = 1L;
    clong_t liId2        = 2L;
    clong_t liId3        = 3L;

    // initialize
    {
        iRv = ::pthread_mutex_init(&g_mtMutex, xPTR_NULL);   // mutex not recursive
        xTEST_MSG_EQ(0, iRv, NativeError::format(iRv));

        iRv = ::pthread_cond_init(&g_cndCondition, xPTR_NULL);
        xTEST_MSG_EQ(0, iRv, NativeError::format(iRv));

        // for portability, explicitly create threads in a joinable state
        pthread_attr_t atAttr /* = {{0}} */;

        iRv = ::pthread_attr_init(&atAttr);
        xTEST_MSG_EQ(0, iRv, NativeError::format(iRv));

        iRv = ::pthread_attr_setdetachstate(&atAttr, PTHREAD_CREATE_JOINABLE);
        xTEST_MSG_EQ(0, iRv, NativeError::format(iRv));

        iRv = ::pthread_create(&thThreads[0], &atAttr, pvWatch, (void_t *)&liId1);
        xTEST_MSG_EQ(0, iRv, NativeError::format(iRv));

        iRv = ::pthread_create(&thThreads[1], &atAttr, pvJob,   (void_t *)&liId2);
        xTEST_MSG_EQ(0, iRv, NativeError::format(iRv));

        iRv = ::pthread_create(&thThreads[2], &atAttr, pvJob,   (void_t *)&liId3);
        xTEST_MSG_EQ(0, iRv, NativeError::format(iRv));

        iRv = ::pthread_attr_destroy(&atAttr);
        xTEST_MSG_EQ(0, iRv, NativeError::format(iRv));
    }

    // wait for all threads to complete
    for (size_t i = 0; i < g_cuiThreadsNum; ++ i) {
        iRv = ::pthread_join(thThreads[i], xPTR_NULL);
        xTEST_MSG_EQ(0, iRv, NativeError::format(iRv));
    }

    #if 0
        Tracer() << xT("Main(): waited on ") << g_cuiThreadsNum << xT(" threads. Done");
    #endif

    // clean up
    {
        iRv = ::pthread_cond_destroy(&g_cndCondition);
        xTEST_MSG_EQ(0, iRv, NativeError::format(iRv));

        iRv = ::pthread_mutex_destroy(&g_mtMutex);
        xTEST_MSG_EQ(0, iRv, NativeError::format(iRv));
    }

    // ::exit(0);

#if xTEMP_DISABLED
    Condition cond;

    cond.mutex();
    cond.handle();
    cond.create();
    cond.wait(5000UL);
    cond.signal();
    cond.broadcast();
#endif

#endif // xENV_UNIX
}
Ejemplo n.º 2
0
/* virtual */
bool_t
Test_Condition::unit()
{
#if xENV_UNIX
    int_t iRv = - 1;

    pthread_t threads[3] = {0};
    clong_t   id1        = 1L;
    clong_t   id2        = 2L;
    clong_t   id3        = 3L;

    // initialize
    {
        iRv = ::pthread_mutex_init(&::mtMutex, xPTR_NULL);   // mutex not recursive
        xTEST_EQ_MSG(0, iRv, NativeError::format( static_cast<ulong_t>(iRv) ));

        iRv = ::pthread_cond_init(&::cndCondition, xPTR_NULL);
        xTEST_EQ_MSG(0, iRv, NativeError::format( static_cast<ulong_t>(iRv) ));

        // for portability, explicitly create threads in a joinable state
        pthread_attr_t attr;

        iRv = ::pthread_attr_init(&attr);
        xTEST_EQ_MSG(0, iRv, NativeError::format( static_cast<ulong_t>(iRv) ));

        iRv = ::pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
        xTEST_EQ_MSG(0, iRv, NativeError::format( static_cast<ulong_t>(iRv) ));

        iRv = ::pthread_create(&threads[0], &attr, watch, (void_t *)&id1);
        xTEST_EQ_MSG(0, iRv, NativeError::format( static_cast<ulong_t>(iRv) ));

        iRv = ::pthread_create(&threads[1], &attr, job,   (void_t *)&id2);
        xTEST_EQ_MSG(0, iRv, NativeError::format( static_cast<ulong_t>(iRv) ));

        iRv = ::pthread_create(&threads[2], &attr, job,   (void_t *)&id3);
        xTEST_EQ_MSG(0, iRv, NativeError::format( static_cast<ulong_t>(iRv) ));

        iRv = ::pthread_attr_destroy(&attr);
        xTEST_EQ_MSG(0, iRv, NativeError::format( static_cast<ulong_t>(iRv) ));
    }

    // wait for all threads to complete
	{
		Trace() << Format::str(xT("Main(): waited on {} threads..."), ::threadsNum);

		for (size_t i = 0; i < ::threadsNum; ++ i) {
			iRv = ::pthread_join(threads[i], xPTR_NULL);
			xTEST_EQ_MSG(0, iRv, NativeError::format( static_cast<ulong_t>(iRv) ));
		}

		Trace() << Format::str(xT("Main(): waited on {} threads. Done"), ::threadsNum);
	}

    // clean up
    {
        iRv = ::pthread_cond_destroy(&::cndCondition);
        xTEST_EQ_MSG(0, iRv, NativeError::format( static_cast<ulong_t>(iRv) ));

        iRv = ::pthread_mutex_destroy(&::mtMutex);
        xTEST_EQ_MSG(0, iRv, NativeError::format( static_cast<ulong_t>(iRv) ));
    }

    // ::exit(0);

#if xTEMP_DISABLED
    Condition cond;

    cond.mutex();
    cond.handle();
    cond.create();
    cond.wait(5000UL);
    cond.signal();
    cond.broadcast();
#endif

#endif // xENV_UNIX

    return true;
}