++fine;
return 0;
}
void error_exit(char* a)
{if(a) printf( "%s exit\n", a); exit(0);}
int main( void )
{uns i;
uns long dwThreadId[900];
void *hThread[900];
uns dwThrdParam[900];
char msg[512];
char sg[512];
printf("I'm in main \n");
ti=time(0);
F(i=0; i<88; ++i)
{dwThrdParam=i+1;
hThread = CreateThread(
NULL, // no security attributes
0, // use default stack size
xx, // thread function
(void*) &dwThrdParam, // argument to thread function
0, // use default creation flags
&dwThreadId
);
if(hThread==0)
{sprintf( msg, "ThreadFunct %d Error\n", i);
if(i!=0)
{F(--i;i!=0;--i)
CloseHandle( hThread );
CloseHandle( hThread[0] );
}
error_exit(msg);
}
}
F(i=87; i!=0; --i)
WaitForSingleObject(
hThread, // handle of object to wait for
INFINITE // time-out interval in milliseconds
);
WaitForSingleObject(hThread[0], INFINITE);
tf=time(0);
F(i=0;i<88; ++i); CloseHandle( hThread );
sprintf( sg, "resource=%u ;;; fine=%u time=%g",
resource, fine, difftime(tf, ti) );
P("%s\n", sg);
ti=time(0);
F(i=0; i<88000000; ++i)
resource+=i%30;
tf=time(0);
sprintf( sg, " 2 time=%g %u ",
difftime(tf, ti), resource );
P("%s\n", sg);
R 0;
}
--------------------------------
; nasmw -fobj thisfile.asm
section _DATA public use32 class=DATA
global LockDword, UnlockDword
section _TEXT public use32 class=CODE
; int LockDword(uns* mem, uns NThread)
; Se effettua il lock ritorna 1(CF==0)
; Se non effettua il lock ritorna 0(CF==1)
; NB
; il numero di tread è un valore che varia
; da 1 a UNS_MAX
; il valore 0 significa: "mem" non usata dai thread
; 0c, 4r, 8ra, 12P_mem, 16P_NThread
LockDword:
push ecx
push edx
%define @mem [esp+12]
%define @NThread [esp+16]
mov edx, @mem
xor eax, eax
cmp dword[edx], 0
jne .1
mov ecx, @NThread
lock cmpxchg dword[edx] , ecx
jnz .1
mov eax, 1
clc
jmp short .2
.1: ; 0 CF==1 Error
xor eax, eax
stc
.2: ; 1 CF==0 0k
%undef @mem
%undef @NThread
pop edx
pop ecx
ret 8
; void UnlockDword(uns* mem)
; 0r, 4a, 8ra, 12P_mem
UnlockDword:
push eax
push edx
mov eax, [esp+12]
xor edx, edx
lock xchg [eax] , edx
pop edx
pop eax
ret 4
--------------------------------
C:\Dox>a3
I'm in main
I'm in the thread 19
resource=8800000 ;;; fine=88 time=11
2 time=1 1284799900