CriticalSection VC
2010-01-04 16:00
447 查看
还是无聊的测试,当线程间会频繁的碰撞时,可能就需要我们仔细的设计同步的检测规则,这个测试说明Interlocked系列函数比CS函数可能更具有优势。所以当我们在多线程间同步时,可以考虑把某些用CS进行同步时的规则合理的转换成 Interlocked函数。
#include "stdafx.h"
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <process.h>
LONG uiCount = 0;
UINT WINAPI IOWorkerThreadProc(LPVOID pParam)
{
DWORD dTick = GetTickCount();
for(int i = 0; i < 100000000; i++)
{
::EnterCriticalSection( &m_cs );
uiCount++;
::LeaveCriticalSection( &m_cs );
}
printf("Thread dead! %d -- %d ",uiCount,GetTickCount()-dTick);
return 0xdead;
}
UINT WINAPI IOWorkerThreadProc_(LPVOID pParam)
{
DWORD dTick = GetTickCount();
for(int i = 0; i < 100000000; i++)
{
InterlockedIncrement(&uiCount);
}
printf("Thread dead! %d -- %d ",uiCount,GetTickCount()-dTick);
return 0xdead;
}
void main()
{
::InitializeCriticalSection( &m_cs );
int i = 0;
for(i = 0; i < 5; i++)
{
_beginthreadex(0,0,IOWorkerThreadProc, 0,0, NULL);
}
getchar();
printf("SUM:%d ",uiCount);
uiCount = 0;
for(i = 0; i < 5; i++)
{
_beginthreadex(0,0,IOWorkerThreadProc_, 0,0, NULL);
}
getchar();
printf("SUM:%d ",uiCount);
getchar();
::DeleteCriticalSection( &m_cs );
}
输出结果:
Thread dead! 420951825 -- 59672
Thread dead! 427460014 -- 60563
Thread dead! 433629330 -- 61391
Thread dead! 498019991 -- 70047
Thread dead! 500000000 -- 70188
SUM:500000000
Thread dead! 416899248 -- 11875
Thread dead! 435609187 -- 12344
Thread dead! 482671736 -- 13750
Thread dead! 499292047 -- 14187
Thread dead! 500000000 -- 14250
SUM:500000000
#include "stdafx.h"
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <process.h>
LONG uiCount = 0;
UINT WINAPI IOWorkerThreadProc(LPVOID pParam)
{
DWORD dTick = GetTickCount();
for(int i = 0; i < 100000000; i++)
{
::EnterCriticalSection( &m_cs );
uiCount++;
::LeaveCriticalSection( &m_cs );
}
printf("Thread dead! %d -- %d ",uiCount,GetTickCount()-dTick);
return 0xdead;
}
UINT WINAPI IOWorkerThreadProc_(LPVOID pParam)
{
DWORD dTick = GetTickCount();
for(int i = 0; i < 100000000; i++)
{
InterlockedIncrement(&uiCount);
}
printf("Thread dead! %d -- %d ",uiCount,GetTickCount()-dTick);
return 0xdead;
}
void main()
{
::InitializeCriticalSection( &m_cs );
int i = 0;
for(i = 0; i < 5; i++)
{
_beginthreadex(0,0,IOWorkerThreadProc, 0,0, NULL);
}
getchar();
printf("SUM:%d ",uiCount);
uiCount = 0;
for(i = 0; i < 5; i++)
{
_beginthreadex(0,0,IOWorkerThreadProc_, 0,0, NULL);
}
getchar();
printf("SUM:%d ",uiCount);
getchar();
::DeleteCriticalSection( &m_cs );
}
输出结果:
Thread dead! 420951825 -- 59672
Thread dead! 427460014 -- 60563
Thread dead! 433629330 -- 61391
Thread dead! 498019991 -- 70047
Thread dead! 500000000 -- 70188
SUM:500000000
Thread dead! 416899248 -- 11875
Thread dead! 435609187 -- 12344
Thread dead! 482671736 -- 13750
Thread dead! 499292047 -- 14187
Thread dead! 500000000 -- 14250
SUM:500000000
相关文章推荐
- 【VC报错】LeaveCriticalSection中断异常
- VC++ 线程同步 CRITICAL_SECTION
- CRITICAL_SECTION 对性能的影响 VC十年技术群聊天笔记
- InitializeCriticalSectionAndSpinCount identifier not found
- CSemaphore/CMutex/CCriticalSection/CEvent区别
- 线程锁的概念函数EnterCriticalSection和LeaveCriticalSection的用法 .
- EnterCriticalSection()
- EnterCriticalSection TryEnterCriticalSection
- InitializeCriticalSectionAndSpinCount
- Windows API一日一练(46)EnterCriticalSection和LeaveCriticalSection函数
- EnterCriticalSection和LeaveCriticalSection函数
- 转:InitializeCriticalSectionAndSpinCount()
- InitializeCriticalSectionAndSpinCount()
- 线程同步问题,CRITICAL_SECTION和Mutex哪个更快
- InitializeCriticalSection
- EnterCriticalSection和LeaveCriticalSection函数
- CRITICAL_SECTION的理解
- 理解EnterCriticalSection 临界区
- 线程锁EnterCriticalSection和LeaveCriticalSection的用法
- EnterCriticalSection的应用说明