使用event与无锁算法实现windows平台下的critical section//#include "own_cs.h"#define WIN32_LEAN_AND_MEAN#include <windows.h>#include <assert.h>#include <malloc.h>#include <stdlib.h>#include <stdio.h>struct own_critical_section{long own_lock_count; // count of locked thread, -1 means unlock , other means lock.HANDLE own_event; // auto-resetDWORD own_owning_thread_id; // owner thread of locklong own_recursion_count; // count of recursion};void InitialOwnCriticalSection( own_critical_section** own_cs){*own_cs = (own_critical_section*)malloc( sizeof( own_critical_section ) );(*own_cs)->own_lock_count = -1;(*own_cs)->own_event = CreateEventW( NULL, FALSE, FALSE, NULL );(*own_cs)->own_owning_thread_id = 0;(*own_cs)->own_recursion_count = 0;}void DeleteOwnCriticalSection( own_critical_section* own_cs ){assert( own_cs != NULL );CloseHandle( own_cs->own_event );free( own_cs );}void EnterOwnCriticalSection( own_critical_section* own_cs ){assert( own_cs->own_lock_count>=-1L );for ( int spin_count = 0; spin_count < 500; ++ spin_count ){//spinlockif ( -1L == InterlockedCompareExchange( &own_cs->own_lock_count, -1L, -1L ) )break;Sleep(0);}DWORD current_thread_id = ::GetCurrentThreadId();if( 0 < InterlockedIncrement( &own_cs->own_lock_count ) &&( own_cs->own_owning_thread_id != current_thread_id ) ){ //lockedWaitForSingleObject( own_cs->own_event, INFINITE ); }own_cs->own_owning_thread_id = current_thread_id;++own_cs->own_recursion_count;}void LeaveOwnCriticalSection( own_critical_section* own_cs ){assert( own_cs->own_lock_count>=-1L );if ( --own_cs->own_recursion_count == 0 )own_cs->own_owning_thread_id = 0;if( -1L != InterlockedDecrement( &own_cs->own_lock_count ) &&(::GetCurrentThreadId() == own_cs->own_owning_thread_id ) ){SetEvent( own_cs->own_event );}}bool TryEnterOwnCriticalSection( own_critical_section* own_cs ){assert( own_cs->own_lock_count>=-1L );DWORD current_thread_id = GetCurrentThreadId();if ( current_thread_id == own_cs->own_owning_thread_id ){ // inside the lockInterlockedIncrement( &own_cs->own_lock_count );return true;}else{ if ( -1L == InterlockedCompareExchange( &own_cs->own_lock_count, 0, -1L ) )return false;own_cs->own_owning_thread_id = current_thread_id;}++own_cs->own_recursion_count;return true;}