` Printed Icetips Article

Icetips Article



Clarion 6: Keep track of all threads
2004-03-12 -- Don Wood
 
Newsgroups: softvelocity.public.clarion6

> Is there a way to loop through all the active threads?
> I need to know if a certain file is in use somewhere in my program before
> it continues.


I would suggest a global, non-threaded queue containing three variables.

!---------------------------------------------------------------------------
INCLUDE('CWSYNCHC.INC'),ONCE    ! needed for critical sections

EVENT:CheckFileForUse         EQUATE (760H)
EVENT:CheckedFileForUse       EQUATE (761H)

MainThread     Signed

ThreadQueue    Queue, pre(TQ)
ThreadNumber     Signed
FileInUse        Byte
CheckedForUse    Byte
               End

ThreadCS       CriticalSection

!---------------------------------------------------------------------------

When the App starts, save it's THREAD() in the MainThread variable.
Then, everytime you start a Proc in a new thread, you add it's Thread to the
queue:

!---------------------------------------------------------------------------

ThreadCS.wait()

clear(ThreadQueue)

TQ:ThreadNumber = Thread()
add(ThreadQueue)

ThreadCS.Release()

!---------------------------------------------------------------------------

Also in the kill embed you'll want to remove the thread number from the
queue.

Now, to find out if any of the threads are using the file, loop through the
queue and post an event to each queue, telling them to check and send a
response.

!---------------------------------------------------------------------------

ThreadCS.wait()

loop i# = 1 to records(ThreadQueue)
    !first clear all the queue's flags
    TQ:FileInUse = FALSE
    TQ:CheckedForUse = FALSE
    put(ThreadQueue)

    ! then tell each thread to check
    post(EVENT:CheckFileForUse, ,TQ:ThreadNumber)
end

ThreadCS.Release()

!---------------------------------------------------------------------------

Then in each thread check for the event in TakeEvent() and check if the file
is in use

!---------------------------------------------------------------------------
case EVENT()
of  EVENT:CheckFileForUse
    ThreadCS.wait()

    clear(ThreadQueue)
    TQ:ThreadNmber = thread()
    get(threadqueue)

    if ~errorcode()
        TQ:CheckedForUse = true

        ! check for file in use in this thread
        ! if file in use
            TQ:FileInUse = true
        else
            TQ:FileInUse = false
        end
        put(ThreadQueue)
    end

    post(EVENT:CheckedFileForUse,,MainThread)
    end

    ThreadCS.Release()
!---------------------------------------------------------------------------

Lastly, in your Main thread you wait for the CheckedFileForUse events.

!---------------------------------------------------------------------------
    ThreadCS.wait()

    loop i# = 1 to records(threadQueue)
        get(ThreadQueue, i#)
        if TQ:CheckedForUse = FALSE
            ! not all threads have responded, might want to break here, 
            ! and wait for another event
        end

        if TQ:FileInUse = TRUE
            ! do what ever you want when the file is in use
        end

    end

    ThreadCS.Release()
!---------------------------------------------------------------------------


It's long and a little confusing, but It's the best way I know of to have
all your threads communicating.

Hope this helps,

Don Wood
VendPrint, Inc.



Printed November 21, 2024, 11:24 am
This article has been viewed/printed 35322 times.
Google search has resulted in 26 hits on this article since January 25, 2004.