看第134到138行代码 //Uncomment one of these to test them //Mutex mux; //running time: 42.25/42.20/42.20 //FastMutex mux; //running time: 3.73/4.48/4.45 //SpinMutex mux; //running time: 0.94/0.94/0.94 NTCritialSection mux; //running time: 42.40/40.80/42.03
我表示看不懂你写的代码 看第134到138行代码 //Uncomment one of these to test them //Mutex mux; //running time: 42.25/42.20/42.20 //FastMutex mux; //running time: 3.73/4.48/4.45 //SpinMutex mux; //running time: 0.94/0.94/0.94 NTCritialSection mux; //running time: 42.40/40.80/42.03
看第134到138行代码 //Uncomment one of these to test them //Mutex mux; //running time: 42.25/42.20/42.20 //FastMutex mux; //running time: 3.73/4.48/4.45 //SpinMutex mux; //running time: 0.94/0.94/0.94 NTCritialSection mux; //running time: 42.40/40.80/42.03 Uncomment one of these to test them 想测试什么的效率,就取消对应行的注释,重新编译运行。其实我想说的是:对效率的要求很高吗?没有特别要求的话,怎么用方便怎么用。最后,代码中的//running time测试结果是参考值,具体得看你使用的计算机。
或者自己用代码测试看看吧。// FastMutex.cpp : Defines the entry point for the console application.
//#include "stdafx.h"/* Define 4 MUTual EXclusion mechanisms:
* Mutex: a plain mutex
* FastMutex: a mutex
* SpinMutex: a spinning mutex
* NTCritical_Section: NT's critical section
*///----------------------------------------------------------------------------
// A plain NT mutex
class Mutex {
HANDLE mux;
public:
Mutex()
: mux(CreateMutex(NULL,FALSE,NULL))
{}
~Mutex()
{ CloseHandle(mux); }
void Request()
{ WaitForSingleObject(mux,INFINITE); }
void Release()
{ ReleaseMutex(mux); }
};//----------------------------------------------------------------------------
// A fast, but friendly, mutex
class FastMutex {
LONG toggle;
LONG waiters;
HANDLE try_again;
public:
FastMutex()
: toggle(0),
waiters(0),
try_again(CreateEvent(NULL,TRUE,FALSE,NULL))
{}
~FastMutex()
{ CloseHandle(try_again); }
void Request();
void Release();
};void FastMutex::Request() {
InterlockedIncrement(&waiters);
for(;;) {
if(InterlockedExchange(&toggle,1)==0) {
InterlockedDecrement(&waiters);
return;
}
WaitForSingleObject(try_again,INFINITE);
ResetEvent(try_again);
}
}void FastMutex::Release() {
toggle=0;
if(waiters!=0)
SetEvent(try_again);
}//----------------------------------------------------------------------------
// A very fast, but friendly, spinning mutex
// This is not a true spinlock since it will block the thread if spinning seems futileclass SpinMutex {
enum {
spin_retries=100 //tailor this value to suit you application/compiler
};
LONG toggle;
LONG waiters;
HANDLE try_again;
public:
SpinMutex()
: toggle(0),
waiters(0),
try_again(CreateEvent(NULL,TRUE,FALSE,NULL))
{}
~SpinMutex()
{ CloseHandle(try_again); }
void Request();
void Release();
};void SpinMutex::Request() {
for(unsigned spin=0; spin<spin_retries; spin++) {
if(InterlockedExchange(&toggle,1)==0)
return;
}
InterlockedIncrement(&waiters);
for(;;) {
if(InterlockedExchange(&toggle,1)==0) {
InterlockedDecrement(&waiters);
return;
}
WaitForSingleObject(try_again,INFINITE);
ResetEvent(try_again);
}
}void SpinMutex::Release() {
toggle=0;
if(waiters!=0)
SetEvent(try_again);
}//----------------------------------------------------------------------------
//NT's critical sections
class NTCritialSection {
CRITICAL_SECTION cs;
public:
NTCritialSection()
{ InitializeCriticalSection(&cs); }
~NTCritialSection()
{ DeleteCriticalSection(&cs); }
void Request() {
EnterCriticalSection(&cs);
}
void Release() {
LeaveCriticalSection(&cs);
}
};// 1: 9.359 9.093 9.125 9.250
// 10: 9.460 9.547 9.266 9.390
// 100: 9.281 9.250 9.500 9.359
//1000: 9.344 9.438 9.406 9.390#include <time.h>
#include <stdio.h>
//Uncomment one of these to test them
//Mutex mux; //running time: 42.25/42.20/42.20
//FastMutex mux; //running time: 3.73/4.48/4.45
//SpinMutex mux; //running time: 0.94/0.94/0.94
NTCritialSection mux; //running time: 42.40/40.80/42.03static inline loop() {
//spend some time ...
for(unsigned i=0; i<1000000; i++) {
mux.Request();
mux.Release();
}
}DWORD WINAPI OtherThread(LPVOID) {
loop();
ExitThread(0);
return 0;
}int main(int,char**)
{
clock_t start=clock(); HANDLE hThread;
DWORD idThread;
//create secondary thread
hThread = CreateThread(NULL, 16384, OtherThread, NULL, 0, &idThread);
//compete with the secondary thread
loop();
//wait for secondary thread to finish
WaitForSingleObject(hThread,INFINITE);
clock_t end=clock();
CloseHandle(hThread);
printf("Running time: %10.3f seconds\n", ((double)(end-start))/CLK_TCK );
return 0;
}
//Uncomment one of these to test them
//Mutex mux; //running time: 42.25/42.20/42.20
//FastMutex mux; //running time: 3.73/4.48/4.45
//SpinMutex mux; //running time: 0.94/0.94/0.94
NTCritialSection mux; //running time: 42.40/40.80/42.03
我表示看不懂你写的代码
看第134到138行代码
//Uncomment one of these to test them
//Mutex mux; //running time: 42.25/42.20/42.20
//FastMutex mux; //running time: 3.73/4.48/4.45
//SpinMutex mux; //running time: 0.94/0.94/0.94
NTCritialSection mux; //running time: 42.40/40.80/42.03
//Uncomment one of these to test them
//Mutex mux; //running time: 42.25/42.20/42.20
//FastMutex mux; //running time: 3.73/4.48/4.45
//SpinMutex mux; //running time: 0.94/0.94/0.94
NTCritialSection mux; //running time: 42.40/40.80/42.03
Uncomment one of these to test them
想测试什么的效率,就取消对应行的注释,重新编译运行。其实我想说的是:对效率的要求很高吗?没有特别要求的话,怎么用方便怎么用。最后,代码中的//running time测试结果是参考值,具体得看你使用的计算机。