📄 unixthread.cpp
字号:
//// This file is part of the "More for C++" library//// Copyright (c) 1999-2003 by Thorsten Goertz (thorsten@morefor.org)//// The "More for C++" library is free software; you can redistribute it and/or// modify it under the terms of the license that comes with this package.//// Read "license.txt" for more details.//// THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED// WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES// OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.////////////////////////////////////////////////////////////////////////////////#include <errno.h>#include <more/create.hpp>#include <more/synchronized.hpp>#include "unixthread.hpp"using namespace more;using namespace more::os;using namespace more::os::unix_;using namespace more::util;////////////////////////////////////////////////////////////////////////////////static void milliSleep( int nMilliseconds ){ size_t nSeconds = nMilliseconds / 1000; size_t nNanoSeconds = ( 1000000 * ( ( nMilliseconds + 1 ) % 1000 ) ); timespec interval = { nSeconds, nNanoSeconds }; int nResult; nResult = nanosleep( &interval, &interval ); while( nResult == -1 && errno == EINTR ) { nResult = nanosleep( &interval, &interval ); }}////////////////////////////////////////////////////////////////////////////////size_t UnixThread::getId( ){ return pthread_self( );}////////////////////////////////////////////////////////////////////////////////void UnixThread::joinRemainingThreads( ){ while( nNoOfRemainingThreads > 0 ) { milliSleep( 100 ); }}////////////////////////////////////////////////////////////////////////////////void UnixThread::sleep( long nMilliseconds){ milliSleep( nMilliseconds );}////////////////////////////////////////////////////////////////////////////////UnixThread::UnixThread( ){ m_thread = 0; m_bScheduled = true;}////////////////////////////////////////////////////////////////////////////////bool UnixThread::scheduled( ){ return m_bScheduled;}////////////////////////////////////////////////////////////////////////////////void UnixThread::execute( const p<Runnable>& pRunnable){ if( m_pRunnable == 0 && pRunnable != 0 ) { nNoOfRemainingThreads++; m_pRunnable = pRunnable; pthread_create( &m_thread, 0, runRunnable, new p<UnixThread>( this ) ); }}////////////////////////////////////////////////////////////////////////////////void UnixThread::terminate( ){ m_bScheduled = false;}////////////////////////////////////////////////////////////////////////////////void UnixThread::join( ){ void* pValuePtr; pthread_join( m_thread, &pValuePtr );}////////////////////////////////////////////////////////////////////////////////UnixThread::UnixController::UnixController( const p<UnixThread>& pUnixThread): m_pUnixThread( pUnixThread ){}////////////////////////////////////////////////////////////////////////////////bool UnixThread::UnixController::scheduled( ){ return m_pUnixThread -> scheduled( );}////////////////////////////////////////////////////////////////////////////////void* UnixThread::runRunnable( void* ppUnixThreadAsVoidPtr){ p<UnixThread>* ppUnixThread = ( p<UnixThread>* ) ppUnixThreadAsVoidPtr; try { ( *ppUnixThread ) -> m_pRunnable -> run( CREATE UnixController( *ppUnixThread ) ); } catch( ... ) { } nNoOfRemainingThreads--; delete ppUnixThread; return 0;}////////////////////////////////////////////////////////////////////////////////size_t UnixThread::nNoOfRemainingThreads = 0;////////////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -