Page 1 of 1

windows callback

Posted: Thu Nov 03, 2005 3:40 pm
by DoubleDutch
Does the windows callback operate like a seperate thread?

The reason I'm asking is that I access a linked list during both the main app and the callback. At the beginning of the callback I save the current list position and restore it at the end.

If it operates like a thread though, this will not be enough.

If so, is there a safe way to access a linked list during the callback or do I have to use a signal/wait method?

-Anthony

Posted: Thu Nov 03, 2005 5:01 pm
by DoubleDutch
Looks like it is dangerous to do this...

Anyhow solved it with a little help from the irc channel... :)

Here is the info, incase some of you have a similar problem:

The list is a structure of a hitem address and a string...
I make SubjectStart the address of the 1st element (if any)...

Code: Select all

; this is in the callback
If SubjectStart
  Unsafelist=#True
  hitem=*tvCD\nmcd\dwItemSpec
  addr=SubjectStart
  textaddr=0
  Repeat
    If PeekL(addr+8)=hitem         ; 1st item in structure
      textaddr=PeekL(addr+12)     ; 2nd item in structure
      If textaddr
        text$=PeekS(textaddr)
      Else
        textaddr=#True
        text$="@"   ; this is for me to see if something is wrong
      EndIf
    Else
      addr=PeekL(addr)       ; next item in list
    EndIf
  Until textaddr Or addr=0
  Unsafelist=#False
Else
  text$="$"  ; list is empty!
EndIf
In the main program i simple use the list as normal, but if I'm deleting an element i wait until Unsafelist=#false before I do it.

If I clear the list I first clear SubjectStart then wait for Unsafelist to be #false.

I think this should be good enough to stop virtually anything going wrong.

Posted: Thu Nov 03, 2005 7:24 pm
by Trond
Just a wild dumb question: How do you ensure that not both threads writes to the flag variable at once?

Posted: Thu Nov 03, 2005 10:44 pm
by DoubleDutch
What I should probabily do is make the flag turing on a critical section. But it will effect system timers, etc...

Notice that I clear SubjectStart before I wait for the flag - thus there is a double protection here.

It seems to work okay, I made it run for quite a while in an extreme situation and nothing flagged up as wrong.

-Anthony