本文主要是介绍boost的多线程实现机理,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
对boost的多线程感觉挺好奇的,所以就追踪了以下。
设置在 < \boost\thread\win32\thread_primitives.hpp > 下面,为了调用windows api,boost是这么做的
# include <windows.h>
namespace boost
{namespace detail{namespace win32{typedef ULONG_PTR ulong_ptr;typedef HANDLE handle;unsigned const infinite=INFINITE;unsigned const timeout=WAIT_TIMEOUT;handle const invalid_handle_value=INVALID_HANDLE_VALUE;unsigned const event_modify_state=EVENT_MODIFY_STATE;unsigned const synchronize=SYNCHRONIZE;# ifdef BOOST_NO_ANSI_APISusing ::CreateMutexW;using ::CreateEventW;using ::OpenEventW;using ::CreateSemaphoreW;
# elseusing ::CreateMutexA;using ::CreateEventA;using ::OpenEventA;using ::CreateSemaphoreA;
# endifusing ::CloseHandle;using ::ReleaseMutex;using ::ReleaseSemaphore;using ::SetEvent;using ::ResetEvent;using ::WaitForMultipleObjects;using ::WaitForSingleObject;using ::GetCurrentProcessId;using ::GetCurrentThreadId;using ::GetCurrentThread;using ::GetCurrentProcess;using ::DuplicateHandle;using ::SleepEx;using ::Sleep;using ::QueueUserAPC;using ::GetTickCount;}}
}
这样做的好处,就是其他头文件用到windows.h,就只需要包含这个头文件了,而且用到什么api函数都列出来了,更清晰吧,后面就是把其中几个函数适配了一下。有些是全局函数来适配的,当然要放命名空间的。还有些用类包装起来了。我看到basic_timed_mutex,其他不清楚,没有总结下去了。
下面几个宏定义:\boost_1_47_0\boost\detail\interlocked.hpp
# include <windows.h># define BOOST_INTERLOCKED_INCREMENT InterlockedIncrement
# define BOOST_INTERLOCKED_DECREMENT InterlockedDecrement
# define BOOST_INTERLOCKED_COMPARE_EXCHANGE InterlockedCompareExchange
# define BOOST_INTERLOCKED_EXCHANGE InterlockedExchange
# define BOOST_INTERLOCKED_EXCHANGE_ADD InterlockedExchangeAdd
# define BOOST_INTERLOCKED_COMPARE_EXCHANGE_POINTER InterlockedCompareExchangePointer
# define BOOST_INTERLOCKED_EXCHANGE_POINTER InterlockedExchangePointer
下面我打算试试看boost的多线程库,是不是方便很多。
这篇关于boost的多线程实现机理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!