Multithreaded programs need a way to synchonize their data access at some risky places. You can always use CriticalSection or Mutex, but what if you try to access an object that no longer exists? EnterCriticalSection does not return a value to tell you if the object exists or not. This can be avoided by initially removing all pointers to the object, but in some cases this won't be of enough security. The following code works perfectly and I have been using it for more than 8 months 12 hours/day, with no fails from it. Twice there were discussions (threads) about this, but few people seemed to use the power of x86 instead of API functions.
I noticed that when you free some heap memory with HeapFree, the first several bytes of that data are still accessible (noticed on Win98SE). Even if they are not, the functions will never fail you. Your program will never lock itself, either.

(see attachment)
Posted on 2003-07-17 14:24:54 by Ultrano
I have used similar code on occasion, though it wasn't as generic. I must confess I don't really know what the lock prefix does, although I can guess. Is there a reason for using a explicit "lock" prefix in the unlocking code, but not the locking code?

PS: If I don't reply, it's because I'm going on vacation tomorrow :) I'll still read this when I get back though.
Posted on 2003-07-18 07:02:06 by Qweerdy
oh, well, at that place the LOCK is useless. I had put it 'just in case' . I am sure it can be removed with no consequences. 'Lock' makes sure no other cpu uses the memory bus during the next instruction, so that no data gets used during the next instruction. Like telling the other cpu to stop for a while (7 cycles I suppose).
Posted on 2003-07-18 07:18:07 by Ultrano
No offense here, but if you're trying to access a synchronization object that don't exist, then you need to seriously rethink your program design.
Posted on 2003-07-18 07:50:42 by iblis
No offense here, but you need to correct you chronology arguments too

February 01, 2003
Ultrano invented the XCHG

http://www.asmcommunity.net/board/showthread.php?threadid=10555&


July 11, 2003
"I have been using XCHG for 1 year, and it absolutely never let me down.
My program never crashed or misbehaved for object locking
thanks to using my own mutex, even though it has 4 threads
that have high priority and each of the threads gets its timeslice
every 10ms."

http://www.asmcommunity.net/board/showthread.php?threadid=14228&highlight=xchg

imho, it is five months rather tnen 1 year


July 17, 2003
"The following code works perfectly and I have been using it for
more than 8 months 12 hours/day,with no fails from it.
Twice there were discussions (threads) about this, but few
people seemed to use the power of x86
instead of API functions."

http://www.asmcommunity.net/board/showthread.php?threadid=14334

just for 7 days 5 months or 1 year become to 8 months
Posted on 2003-07-18 11:05:20 by lingo12
you have a point, but you forgot to bold 12 hours/day ;)
Posted on 2003-07-18 12:30:56 by Ultrano