#include <windows.h>
#include <stdio.h>
#define ALLNUM 10000000
void testlock()
{
int timecode = GetTickCount();
for(long i=0; i<ALLNUM; InterlockedIncrement(&i))
;
printf("locked model use time %d/n", GetTickCount() - timecode);
}
void testunlock()
{
int timecode = GetTickCount();
for(long i=0; i<ALLNUM; i++)
;
printf("unlock model use time %d/n", GetTickCount() - timecode);
{
int timecode = GetTickCount();
for(long i=0; i<ALLNUM; i++)
;
printf("unlock model use time %d/n", GetTickCount() - timecode);
}
HANDLE sema;
void testcore()
{
CreateSemaphore(NULL, 1, 1, NULL);
int timecode = GetTickCount();
for(long i=0; i<ALLNUM/999;)
&
void testcore()
{
CreateSemaphore(NULL, 1, 1, NULL);
int timecode = GetTickCount();
for(long i=0; i<ALLNUM/999;)
&

本文通过示例代码对比了不同线程同步方法的效率,包括InterlockedIncrement函数的使用。总结指出,临界区在无冲突时接近用户级别操作,冲突时则涉及系统级别,影响速度。临界区在无冲突时的速度大约是锁定操作的2-3倍,但严重冲突时会增加系统调用的时间成本。
最低0.47元/天 解锁文章
1934

被折叠的 条评论
为什么被折叠?



