"menchar" wrote:
> A Sleep() or SleepEx() call isn't inherently "dangerous" ... A suspend or sleep forever call is of course potentially
> dangerous ...
Good. That's what I figured, but I wanted to be clear on that. If CVI's handling of user-interface events (keyboard
activity, mouse activity) is anything like how the Win32 API does it, the events are put in queues, so even if some
events happen while a thread is ZZZ, I assume that the thread, if it's looking for events at all, would pick up its
messages on each wakeup. I'm depending on my "main()" thread to do just that, as it has the only call in the
whole program to ProcessSystemEvents(), which is called once every 100ms.
> What keeps one thread from reading the integer, deciding to set it, but another thread decides to do the same
> thing and they both read 0, then both write 1, and then both proceed thinking they have the lock, and then get hung
> up on trying to access the same resource?
Two things:
1. Even if two threads tried to do this, to hang-up, they'd have to attempt to grab the same resource simultaneously,
to within about a nanosecond (one or two CPU clock cycles), else one would set the lock before the other tested it.
For comm events that are happening once every 10 seconds or so, the probability of this is about 1 in 10 trillion.
2. The only functions in the program attempting to access a comm port are in the "Communications" thread.
And that thread doesn't launch any further threads, so once it executes a comm function, that function will hog the
thread until it completes and releases its lock. So the locks are really just safeguards against the unknown. (Such as
future addition of other threads which may attempt communications.)
> ... critical section ... interlocked variable ... event, mutex, or semaphore ...
All very useful, I'm sure, for multiple threads fighting over same resources; but at present, I think my other,
simpler ways of handling this will suffice. If I really do get 2 or more threads fighting over a resource, i'll
have to look into those.
Seems to me that to be 100% sure of a lock, the locking mechanism would have to be part of the resource being
vied for, not part of the competitors. The resource itself can then say, "Thread Argle, sorry, but thread Bargle made
a request 2.3ns before you did, so you'll have to wait." Argle then has to keep trying untill the resource's arbitor says
"Argle, you are now cleared for access." When Argle is done, it says "Finished, thanks" and the resource releases
the lock.
> My experience with the CVI debugger is that it breaks with whichever thread first hits the breakpoint
Ok.
> you can see the thread ID I think so you know which one it was.
I know what functions are in what thread by the call tree: a function is always in the thread that called it,
and I'm only starting two threads on startup, and only one calls a bunch of functions.
> If you're suspeneded for a breakpoint and a second thread tries to execute the breakpoint, I'm not sure
> what happens - I imagine the second thread might suspend waiting for the frst thread to clear the breakpoint,
> or it might proceed past it. NI can tell you I'm sure.
I'm pretty sure that the threads continue, unless they, too, hit a breakpoint, in which case they halt.
I think now that the "deadlock" problem I've been having is due to one or more threads other than the "active"
thread (the one showing in red in the Run/Threads list) being halted at breakpoints. The breakpoint is
highlighted, but in pale-grey instead of in bright-red, and tapping the F5 key has no effect. I haven't figured
out how to switch the "grey" thread to "active" yet.
> It could be you're running on a multi-core processor with true concurrency, and you're encountering some
> nuance in the CVI debugger code. You can set the boot.ini file to force the use of a single core for all threads
> to see if that's what's happening.
Good idea. I'll try that.
> I suggest reading Multithreading Applications in Win32, Beveridge and Weiner, ISBN 0-201-44234-5.
> Old book now but no better available.
Thanks for the reference. Sounds like something I should read, as I get more into multi-threaded programming.
--
Cheers,
Robbie Hatley
lonewolf aatt well dott com
www dott well dott com slant user slant lonewolf slant