darkblacklife wrote:in order to wait to some change in a shared variable, I was using this code:
Code: Select all
MutexLock var_sync
while var > 1
MutexUnLock var_sync
sleep(10)
MutexLock var_sync
wend
MutexUnLock var_sync
But then I "discovered" conditional objects and I replaced it for this other one:
Code: Select all
mutexlock var_sync
while var > 1
condwait var_cond, var_sync
wend
mutexunlock var_sync
Well,... a lot of things like this in a source code
I was thinking new code should use cpu less than first code... because it avoid false cycle checks, do same thing with less cycles.... but when i try some test Windows tell me that new program with new code using conditional objects needs more cpu and more memory...
So.... when should I use conditional objects??? and why the usage of it needs more cpu??
thanks
As I understand it, you are not suing CONDWAIT correctly. It does not require a loop. With CONDWAIT you wait on a handle until it is signalled from another thread, with no loop required, the thread with CONDWAIT just freezes. Perhaps the example in the help file is overly complex, confusing rather than educating.
I have not used it for a long time, so I will try some code to see if I can explain how I think it should work.
Later
Garvan
PS.
This is a sample of how I think it should be used.
Code: Select all
dim shared as any ptr hcond, hmutex, thread
sub testing123(d As Any Ptr)
print "In testing123 sub"
mutexlock(hmutex)
CondWait(hcond, hmutex)
mutexunlock(hmutex)
print "Exiting testing123 sub"
end sub
dim as string s
hcond = CondCreate()
hmutex = MutexCreate()
print "starting test"
Print "Press SPACE to exit thread, ESC to exit main program."
thread = ThreadCreate( @testing123, 0 )
do
s = inkey
if s = " " then
print "Signaling"
mutexlock(hmutex)
CondSignal ( hcond )
mutexunlock(hmutex)
end if
loop until s = chr(27)
MutexDestroy hmutex
CondDestroy hcond
Note that CondWait automatically unlocks hmutex, allowing CondSignal to be executed and it would be better to add code to exit the thread if ESC is pressed before SPACE.