tthreads.cpp

来自「symbian 下的helix player源代码」· C++ 代码 · 共 520 行 · 第 1/2 页

CPP
520
字号
    //Now we have the mutex locked 2 times, spawn a thread and make
    //sure it can't lock it.
    HXThread::MakeThread(pThread);
    if( !pThread || FAILED(res) )
    {
        fprintf( stderr, "FAILED:Failed to make thread for mutex test.\n" );
        return 1;
    }
    //Thread func 2 will just try and lock the mutex we pass in.
    //It better block until we release it 2 times...
    nFlag = 0;
    pThread->CreateThread(_threadFunc2, (void*)pMutex );
    
    //make sure the thread has a chance to run and set the magic number.
    sleep(1);
    fprintf( stderr, "test thread should now be blocked...\n" );
    if( nFlag != MAGIC_NUMBER )
    {
        fprintf( stderr, "FAILED:test thread did no block on locked mutex...\n" );
        return 1;
    }
    
    fprintf( stderr, "Unlocking the mutex once...test thread should not be free.\n" );
    pMutex->Unlock();
    sleep(1);

    if( nFlag != MAGIC_NUMBER )
    {
        fprintf( stderr, "FAILED:mutex lock count does not work...\n" );
        return 1;
    }
    
    fprintf( stderr, "Unlocking the mutex again. This should free test thread..\n" );
    pMutex->Unlock();
    sleep(1);
    if( nFlag != 0 )
    {
        fprintf( stderr, "FAILED: test thread should have been unlocked....\n" );
        return 1;
    }
    
    HX_DELETE( pThread );
    HX_DELETE( pMutex );
    
        
    ////////////////////////
    //
    // EVENT TESTS
    //
    ////////////////////////
     HXEvent* pEvent = NULL;
    HXEvent::MakeEvent(pEvent, "foo", FALSE);

    if( !pEvent )
    {
        fprintf( stderr, "FAILED: Can't make events...\n" );
        return 1;
    }
    
    //Non manual resets events. Only one thread should wake up each signal.
     HXThread* pThread1 = NULL;
    HXThread* pThread2 = NULL;
    
    HXThread::MakeThread(pThread1);
    HXThread::MakeThread(pThread2);
    HXMutex::MakeMutex(pMutex); //to protect out flag.
    if( !pThread1 || !pThread2 || !pMutex )
    {
        fprintf( stderr, "FAILED:Failed to make thread for Event test.\n" );
        return 1;
    }
    nFlag = 0;
    
    //Thread func 2 will just try and lock the mutex we pass in.
    //It better block until we release it 2 times...
    res = pThread1->CreateThread(_threadFuncEvent1, (void*)pEvent );
    if( FAILED(res) )
    {
        fprintf( stderr, "FAILED: Can't create Event Thread1\n" );
        return 1;
    }
    
    res = pThread2->CreateThread(_threadFuncEvent2, (void*)pEvent );
    if( FAILED(res) )
    {
        fprintf( stderr, "FAILED: Can't create Event Thread2\n" );
        return 1;
    }

    sleep(1);
    //Both threads should be waiting on us to signal the event then.
    if( nFlag != 0 )
    {
        fprintf( stderr, "FAILED: nFlag should be zero.\n" );
        return 1;
    }
    //Let one thread go...
    pEvent->SignalEvent();
    sleep(1);
    if( nFlag != 1 )
    {
        //Either the thread didn't go or both went. Either way its
        //bad.
        fprintf( stderr, "FAILED: nFlag should be 1.\n" );
        return 1;
    }

    //Let the next thread go...
    pEvent->SignalEvent();
    sleep(1);
    if( nFlag != 2 )
    {
        //Either the thread didn't go or both went. Either way its
        //bad.
        fprintf( stderr, "FAILED: nFlag should be 2.\n" );
        return 1;
    }

    HX_DELETE(pEvent);
    HX_DELETE(pThread1);
    HX_DELETE(pThread2);
    //Now test manual reset ones...
    HXEvent::MakeEvent(pEvent, "foo", TRUE);

    if( !pEvent )
    {
        fprintf( stderr, "FAILED: Can't make events...\n" );
        return 1;
    }
    
    //Non manual resets events. Only one thread should wake up each signal.
    HXThread::MakeThread(pThread1);
    HXThread::MakeThread(pThread2);
    if( !pThread1 || !pThread2 || !pMutex )
    {
        fprintf( stderr, "FAILED:Failed to make thread for Event test.\n" ); 
        return 1;
    }
    nFlag = 0;
    
    //Thread func 2 will just try and lock the mutex we pass in.
    //It better block until we release it 2 times...
    pThread1->CreateThread(_threadFuncEvent1, (void*)pEvent );
    pThread2->CreateThread(_threadFuncEvent2, (void*)pEvent );

    sleep(1);
    //Both thread should be waiting on the broadcast...
    if( nFlag != 0 )
    {
        fprintf( stderr, "FAILED: nFlag should be zero.\n" );
        return 1;
    }
    //Let all threads go...
    pEvent->SignalEvent();
    sleep(1);
    
    if( nFlag != 2 )
    {
        fprintf( stderr, "FAILED: both threads should have gone..\n" );
        return 1;
    }
    
    HX_DELETE(pEvent);
    HX_DELETE(pThread1);
    HX_DELETE(pThread2);
    HX_DELETE(pMutex);


    ////////////////////////
    //
    // ASYNC TIMER TESTS
    //
    ////////////////////////

    //Try a timer that calls a proc first....
    fprintf( stderr, "You should see our timer proc fire every second...\n" );
    nFlag = 0;
    int id = HXAsyncTimer::SetTimer( 1000, _timerProc1 );

    sleep(5);
    HXAsyncTimer::KillTimer(id);
    int ttt = nFlag;
    fprintf( stderr, "You should see no more timer procs....\n" ); 
    sleep(3);

    //The timer proc incrments nFlag each time it is called. So, since
    //we slept for 5 seconds and we were suppose to call it every second
    //I will take it as a fail if it hasn't at least been incremnted
    //at least once. Also, if nFlag gets incremented again it didn't kill
    //correctly.
    if( 0==ttt )
    {
        fprintf( stderr, "FAILED: The timer proc didn't ever fire.\n" );
        return 1;
    }
    if( nFlag!=ttt )
    {
        fprintf( stderr, "FAILED: The timer proc didn't stop firing when we killed it.\n" );
        return 1;
    }
    
    
    //Now try posting timer messages to a thread....
    HXThread::MakeThread(pThread1);
    if( !pThread1  )
    {
        fprintf( stderr, "FAILED:Failed to make thread for Timer test.\n" ); 
        return 1;
    }
    nFlag = 0;
    
    //Send it a few messages. When it gets 5 messages it will signal
    //this event that we are waiting on and then we will kill the timer
    //and send it a quit event to it can die.
    HXEvent::MakeEvent(pEvent, "foo", TRUE);
    if( !pEvent )
    {
        fprintf( stderr, "FAILED: Can't make event for timer test.\n" );
        return 1;
    }

    ST_STUFF stuff;
    stuff.pEvent = pEvent;
    stuff.pThread = pThread1;
    
        
    pThread1->CreateThread(_timerProc2, (void*)&stuff );
    id = HXAsyncTimer::SetTimer( 1000, pThread1 );
    pEvent->Wait();
    //Verify that the thread got at least 5 async messages.
    if( nFlag < 5 )
    {
        fprintf( stderr, "FAILED: _timerProc2 didn't get enough messages.\n" );
        return 1;
    }
    
    //Kill the timer before we destroy the thread.
    HXAsyncTimer::KillTimer(id);
    
    //When the thread exits it will set the nFlag back to zero...
    HXThreadMessage msgQuit(HXMSG_QUIT, NULL, NULL);
    pThread1->PostMessage( &msgQuit );

    //Join the thread.
    pThread1->Exit(0);
    if( nFlag != 0 )
    {
        fprintf( stderr, "FAILED: Thread didn't quit correctly\n" );
        return 1;
    }
    HX_DELETE( pThread1 );

    fprintf( stderr, "PASSED!\n" ); 
    return 0;
}



}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?