📄 win32thread.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.////////////////////////////////////////////////////////////////////////////////#ifndef _WIN32#define _WIN32#endif#ifndef _MT#define _MT#endif#include <windows.h>#include <process.h>#include <more/create.hpp>#include <more/synchronized.hpp>#include "win32thread.hpp"using namespace more::os::win32;////////////////////////////////////////////////////////////////////////////////void Win32Thread::joinRemainingThreads( ){ while( nNoOfRemainingThreads > 0 ) { sleep( 100 ); }}////////////////////////////////////////////////////////////////////////////////Win32Thread::Win32Thread( ){ m_nThreadId = 0; m_hThreadHandle = INVALID_HANDLE_VALUE; m_bScheduled = true; m_pCondition = Condition::create( );}////////////////////////////////////////////////////////////////////////////////void Win32Thread::finalize( ){ if( m_hThreadHandle != INVALID_HANDLE_VALUE ) { CloseHandle( m_hThreadHandle ); }}////////////////////////////////////////////////////////////////////////////////bool Win32Thread::scheduled( ){ return m_bScheduled;}////////////////////////////////////////////////////////////////////////////////void Win32Thread::execute( const p<Runnable>& pRunnable){ if( m_pRunnable == 0 && pRunnable != 0 ) { nNoOfRemainingThreads++; m_pRunnable = pRunnable; m_hThreadHandle = ( HANDLE ) _beginthreadex( NULL, 0, ( unsigned int ( __stdcall* )( void* ) ) run, new p<Win32Thread>( this ), 0, ( unsigned int* ) &m_nThreadId ); }}////////////////////////////////////////////////////////////////////////////////void Win32Thread::terminate( ){ m_bScheduled = false;}////////////////////////////////////////////////////////////////////////////////void Win32Thread::join( ){ synchronized( m_pCondition ) { m_pCondition -> wait( ); } end_synchronized}////////////////////////////////////////////////////////////////////////////////Win32Thread::Win32Controller::Win32Controller( const p<Win32Thread>& pWin32Thread): m_pWin32Thread( pWin32Thread ){}////////////////////////////////////////////////////////////////////////////////bool Win32Thread::Win32Controller::scheduled( ){ return m_pWin32Thread -> scheduled( );}////////////////////////////////////////////////////////////////////////////////DWORD WINAPI Win32Thread::run( p<Win32Thread>* ppWin32Thread){ synchronized( ( *ppWin32Thread ) -> m_pCondition ) { try { ( *ppWin32Thread ) -> m_pRunnable -> run( CREATE Win32Controller( *ppWin32Thread ) ); } catch( ... ) { } ( *ppWin32Thread ) -> m_pCondition -> notify( ); } end_synchronized nNoOfRemainingThreads--; delete ppWin32Thread; return 0;}////////////////////////////////////////////////////////////////////////////////size_t Win32Thread::nNoOfRemainingThreads = 0;////////////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -