WD The threads (pool) - Thread das Galinhas - Semáforo de Pause de Execução - otimo exemlo de uso e estudo de threads para os seus sistemas
PROCEDURE MyWindow()
GLOBAL
gnThreadNum is int // Number of the last created thread
gaaThreadIndex is associative array of int
gbStop is boolean // Global boolean to ask the threads to stop
garrPosition is array <growth=N> of Point
gnObjectWidth is int = IMG_Pool..Width
gnObjectHeight is int = IMG_Pool..Height
gnAreaWidth is int = IMG_BACKGROUND..Width
CONSTANT
BASE_THREAD_NAME = "BubbleThread"
SEM_LIMIT = "SemLimit"
SC_STOP_ = "CriticalSectionStop"
SC_ARRPOSITION = "garrPosition"
END
// threadCriticalSection indicates to WLanguage that several threads can use the same procedure at the same time
ThreadMode(threadCriticalSection)
// Initialize the engine for random generation
InitRandom()
-----------------------------------------------------------------------------------------
HourGlass(True)
WaitForThreadStop()
// Start from the first thread
gnThreadNum = 1
// Delete the former semaphore (if any)
SemaphoreDestroy(SEM_LIMIT)
// Recreate the semaphore
// This is a general semaphore for managing the pool of threads.
// Specify in parameter the maximum number of uses for the semaphore, which means the maximum number of threads
SemaphoreCreate(SEM_LIMIT,SLD_LD_NB_THREAD..Value)
// Ungray the button for bubble creation
BTN_New_object..State = Active
// Modify the number of threads in the pool
GR_AnimInProgress..State = Active
HourGlass(False)
Info("The appl
//-------------------------------------------------------------------------------------------------------------------------------------
HourGlass(True)
// Unable to modify the number of threads in the pool
GR_AnimInProgress..State = Grayed
// Adds the thread into the table
gaaThreadIndex[gnThreadNum] = TableAddLine(TABLE_Threads,gnThreadNum,"")
nMaxNb is int = (IMG_BACKGROUND..Height / gnObjectHeight)
nInitYPos is int = modulo(gnThreadNum,nMaxNb) * gnObjectHeight
// Run the thread
ThreadExecute(BASE_THREAD_NAME+gnThreadNum,threadNormal,ThreadAnimation,gnThreadNum,0,nInitYPos)
// Wait for the thread to be started
ThreadWaitSignal()
// When creating the first thread, trigger the drawing timer as well
IF gnThreadNum=1 THEN
TimerSys(Drawing,10,1)
END
// Increment the number of the current thread
gnThreadNum ++
STC_Static1 = "You now have the ability to create objects. To erase them and to stop all the corresponding threads, click the ""Stop-Erase"" button."
HourGlass(False)
//-------------------------------------------------------------------------------------------------------------
HourGlass(True)
// Stop all the threads
WaitForThreadStop()
// Unable to create new bubble
BTN_New_object..State = Grayed
EndTimerSys(1)
// Deletes the current drawing
cImg is an Image
cImg..FillColor = White
cImg..Width = IMG_BACKGROUND..Width
cImg..Height = IMG_BACKGROUND..Height
IMG_BACKGROUND = cImg