Threads and conditional objects...

General FreeBASIC programming questions.
Post Reply
darkblacklife
Posts: 149
Joined: Jun 18, 2008 22:10

Threads and conditional objects...

Post by darkblacklife »

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
SSC
Posts: 319
Joined: May 29, 2005 4:47
Location: Around
Contact:

Post by SSC »

I personally dont use thread conditions, but if you want to use a thread to check if a certain variable has changed you could do this:

Code: Select all

sub some_thread()
do
  if var_1 = somevalue then
    var_2 = some_other_value
  end if
  sleep 1000
loop until program_end = true
end sub
this will check once per second (or whatever timing you want - having very low cpu usage based on said timer) and check to see if var_1 is what you want it to be.
Sisophon2001
Posts: 1706
Joined: May 27, 2005 6:34
Location: Cambodia, Thailand, Lao, Ireland etc.
Contact:

Re: Threads and conditional objects...

Post by Sisophon2001 »

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.
darkblacklife
Posts: 149
Joined: Jun 18, 2008 22:10

Re: Threads and conditional objects...

Post by darkblacklife »

Excuseme for my bad english

I wrote in first post the most simple example... In that case it is waiting until some variable race to some value

In other cases I'm using a loop with conwait to cleaning some buffer when it is finished to use....

maybe it looks like a complex way to do a memory deallocation, but I have a library (CQBS in project section of this forum) that needs clean some buffers and some windows Api internal type to manage block of data sound... but it can't clean these inside the callback function to use when sound is finish because it need call some functions (like unprepareheader) that wait until callback function finish.... so I need to use a parallel thread that use these functions when the callback function changes some variable o something like this.....

In that parallel thread I was using a loop with a sleep(10) delay and mutex or criticalsections to check this shared variable.... I was thinking that the usage of conditional objects should take less cycles than original code because only would run a cycle when variable changes.... but the usage of condwait need more cpu time
Post Reply