From: io_x on
How you from win32 can lock one resource used from many treadhs?
example:

Lock(&resource);
doing_something_only_one_thread_can_do();
FreeTheLock(&resource);

e.g
int resourceMem=0;

void* malloc(unsigned s)
{void *v;
....
Lock(&resourceMem);
....
FreeTheLock(&resourceMem);
return v;
}

how one can use semaphores instead?

What obout
int res;
BloccaRisorsa(&res);
doing_something_only_one_thread_can_do();
LiberaRisorsa(&res);

where

; void __stdcall BloccaRisorsa(u32* Risorsa)
; serve per usare una risorsa quando + thread vogliono accedere
align 4
BloccaRisorsa:
..1: mov ecx, dword[esp+ 4]
xor eax, eax
mov edx, 1
lock CMPXCHG dword[ecx], edx
jnz .2 ; ed ZF==1, altrimenti ZF==0
push 0
call Sleep ; Sleep(0) seems ok
jmp short .1
..2:
ret 4


; void __stdcall LiberaRisorsa(u32* Risorsa)
; serve per liberare una risorsa gia' usata
align 4
LiberaRisorsa:
pushad
pushfd
..1: mov ecx, dword[esp+ 40]
xor eax, eax
lock XCHG dword[ecx], eax
..2:
popfd
popad
ret 4

and Sleep() is the one with win32?

How many error there are above?



From: Dee Earley on
On 20/04/2010 10:50, io_x wrote:
> How you from win32 can lock one resource used from many treadhs?
> example:

CriticalSections maybe?
Have a look in MSDN for the synchronisation methods too.

--
Dee Earley (dee.earley(a)icode.co.uk)
i-Catcher Development Team

iCode Systems

(Replies direct to my email address will be ignored.
Please reply to the group.)
From: io_x on

"io_x" <a(a)b.c.invalid> ha scritto nel messaggio
news:4bcd76d4$0$1128$4fafbaef(a)reader3.news.tin.it...
> How you from win32 can lock one resource used from many treadhs?
> example:
>
> Lock(&resource);
> doing_something_only_one_thread_can_do();
> FreeTheLock(&resource);
>
> e.g
> int resourceMem=0;
>
> void* malloc(unsigned s)
> {void *v;
> ...
> Lock(&resourceMem);
> ...
> FreeTheLock(&resourceMem);
> return v;
> }
>
> how one can use semaphores instead?
>
> What obout
> int res;
> BloccaRisorsa(&res);
> doing_something_only_one_thread_can_do();
> LiberaRisorsa(&res);

yes this above should be not ok if tread are >2
for example one thread get always
the resource first than all other
what about this?

int fifoRes;

int Lock(u32 *m)
{int h=getThisThreadhanle(), hh, m;
BloccaRisorsa(&fifoRes);
m=pushFifo(fifo, h);
LiberaRisorsa(&fifoRes);
if(m==0) return 0; // fifo full
if(m!=1) Sleep(0); // is not good that pufhFifo()
// returns the number of elements in it?
a:
BloccaRisorsa(&fifoRes);
m=readTheFirst(&hh, fifo);
if(hh==h){ BloccaRisorsa(m); // this should wait until "m" is not used
// and not allow other thread to read-write it
popFifo(&hh, fifo);
LiberaRisorsa(&fifoRes);
return 1;
}
LiberaRisorsa(&fifoRes);
Sleep(0);
if(m!=0) goto a; // m==0 means fifo void
return 0;
}

using like
void* malloc(unsigned s)
{void *v;
...
if(Lock(&resourceMem)==0) return 0;
...
LiberaRisorsa(&resourceMem);
return v;
}




From: io_x on

"Dee Earley" <dee.earley(a)icode.co.uk> ha scritto nel messaggio
news:4bcd7dff$0$346$7b0f0fd3(a)reader.news.newnet.co.uk...
> On 20/04/2010 10:50, io_x wrote:
>> How you from win32 can lock one resource used from many treadhs?
>> example:
>
> CriticalSections maybe?

maybe not, the last time i use it in my home made malloc() function
with only one thread
the program increase the memory in function of the time until sys slow
down or seg fault if i remember well

> Have a look in MSDN for the synchronisation methods too.




From: ScottMcP [MVP] on

malloc is thread safe in all modern versions of the C runtime
library. But it was not thread safe in some versions > 10 years ago.