Revision history for KeyPgThreadWait


Revision [22064]

Last edited on 2017-12-29 04:34:53 by fxm [Added advice to avoid the memory leaks]
Additions:
In order to avoid memory leaks, the safe way to end a thread is to always signal to it that it must end, and then call **""ThreadWait""** on that thread except if ##[[KeyPgThreadDetach|ThreadDetach]]## has previously been called.


Revision [21510]

Edited on 2016-07-12 13:01:10 by fxm [Added precision]
Additions:
**""ThreadWait""** waits for a thread created by ##[[KeyPgThreadCreate|ThreadCreate]]## or ##[[KeyPgThreadCall|ThreadCall]]## to finish execution, and then releases the resources associated with the thread handle. **""ThreadWait""** does not return until the thread designated by ##//id//## ends.
During the wait, no CPU time is consumed by the caller.
Deletions:
**""ThreadWait""** waits for a thread created by ##[[KeyPgThreadCreate|ThreadCreate]]## or ##[[KeyPgThreadCall|ThreadCall]]## to finish execution, and then releases the resources associated with the thread handle. **""ThreadWait""** does not return until the thread designated by ##//id//## ends.


Revision [20552]

Edited on 2016-02-10 16:09:31 by DkLwikki [Update link format]
Additions:
[[KeyPgDeclare|Declare]] [[KeyPgSub|Sub]] **""ThreadWait""** ( [[KeyPgByval|Byval]] //id// [[KeyPgAs|As]] [[KeyPgAny|Any]] [[KeyPgPtr|Ptr]] )
##[[KeyPgAny|Any]] [[KeyPgPtr|Ptr]]## handle of a thread created by ##[[KeyPgThreadCreate|ThreadCreate]]## or ##[[KeyPgThreadCall|ThreadCall]]##
**""ThreadWait""** waits for a thread created by ##[[KeyPgThreadCreate|ThreadCreate]]## or ##[[KeyPgThreadCall|ThreadCall]]## to finish execution, and then releases the resources associated with the thread handle. **""ThreadWait""** does not return until the thread designated by ##//id//## ends.
In order to release a thread handle without waiting for the thread to finish, use ##[[KeyPgThreadDetach|ThreadDetach]]##.
See the ##[[KeyPgThreadCreate|Threadcreate]]## examples.
- Threading is not allowed in the //[[CompilerOptlang|-lang qb]]// dialect.
- ##[[KeyPgThreadCreate|ThreadCreate]]##
- ##[[KeyPgThreadDetach|ThreadDetach]]##
Deletions:
[[KeyPgDeclare Declare]] [[KeyPgSub Sub]] **""ThreadWait""** ( [[KeyPgByval Byval]] //id// [[KeyPgAs As]] [[KeyPgAny Any]] [[KeyPgPtr Ptr]] )
##[[KeyPgAny Any]] [[KeyPgPtr Ptr]]## handle of a thread created by ##[[KeyPgThreadCreate ThreadCreate]]## or ##[[KeyPgThreadCall ThreadCall]]##
**""ThreadWait""** waits for a thread created by ##[[KeyPgThreadCreate ThreadCreate]]## or ##[[KeyPgThreadCall ThreadCall]]## to finish execution, and then releases the resources associated with the thread handle. **""ThreadWait""** does not return until the thread designated by ##//id//## ends.
In order to release a thread handle without waiting for the thread to finish, use ##[[KeyPgThreadDetach ThreadDetach]]##.
See the ##[[KeyPgThreadCreate Threadcreate]]## examples.
- Threading is not allowed in the //[[CompilerOptlang -lang qb]]// dialect.
- ##[[KeyPgThreadCreate ThreadCreate]]##
- ##[[KeyPgThreadDetach ThreadDetach]]##


Revision [17066]

Edited on 2014-04-28 08:43:20 by DkLwikki [Reformat and mention threaddetach]
Additions:
Waits for a thread to finish execution and releases the thread handle
Deletions:
Waits until the designated thread has been completed before returning


Revision [17065]

Edited on 2014-04-28 08:42:32 by DkLwikki [Reformat and mention threaddetach]
Additions:
[[KeyPgDeclare Declare]] [[KeyPgSub Sub]] **""ThreadWait""** ( [[KeyPgByval Byval]] //id// [[KeyPgAs As]] [[KeyPgAny Any]] [[KeyPgPtr Ptr]] )
**""ThreadWait""**( //id// )
##[[KeyPgAny Any]] [[KeyPgPtr Ptr]]## handle of a thread created by ##[[KeyPgThreadCreate ThreadCreate]]## or ##[[KeyPgThreadCall ThreadCall]]##
**""ThreadWait""** waits for a thread created by ##[[KeyPgThreadCreate ThreadCreate]]## or ##[[KeyPgThreadCall ThreadCall]]## to finish execution, and then releases the resources associated with the thread handle. **""ThreadWait""** does not return until the thread designated by ##//id//## ends.

In order to release a thread handle without waiting for the thread to finish, use ##[[KeyPgThreadDetach ThreadDetach]]##.

**""ThreadWait""** does not force the thread to end; if a thread requires a signal to force its end, a mechanism such as shared variables and mutexes must be used.
- **""ThreadWait""** is not available with the DOS version of ""FreeBASIC"", because multithreading is not supported by DOS kernel nor the used extender.
- ##[[KeyPgThreadCreate ThreadCreate]]##
- ##[[KeyPgThreadDetach ThreadDetach]]##
Deletions:
[[KeyPgDeclare declare]] [[KeyPgSub sub]] **Threadwait** ( [[KeyPgByval byval]] //id// [[KeyPgAs as]] [[KeyPgAny any]] [[KeyPgPtr ptr]] )
**Threadwait**( //id// )
##[[KeyPgAny any]] [[KeyPgPtr ptr]]## handle of a thread created by ##[[KeyPgThreadCreate Threadcreate]]##
##Threadwait## doesn't return until the thread designated by ##//id//## ends. ##Threadwait## does not force the thread to end; if a thread requires a signal to force its end, a mechanism such as shared variables must be used.
Threads are launched by the ##[[KeyPgThreadCreate Threadcreate]]## function.
To avoid simultaneous access to shared resources from different threads, ""FreeBASIC"" implements mutexes, mutual exclusion locks that can be "owned" by a single thread when doing critical work. See ##[[KeyPgMutexCreate Mutexcreate]]##, ##[[KeyPgMutexLock Mutexlock]]##, ##[[KeyPgMutexUnlock Mutexunlock]]##, ##[[KeyPgMutexDestroy Mutexdestroy]]##
- **Threadwait** is not available with the DOS version of ""FreeBASIC"", because multithreading is not supported by DOS kernel nor the used extender.
- In Linux the threads are always started in the order they are created, this can't be assumed in ""Win32"". It's an OS, not a ""FreeBASIC"" issue.
- ##[[KeyPgThreadCreate Threadcreate]]##
- ##[[KeyPgMutexCreate Mutexcreate]]##
- ##[[KeyPgMutexLock Mutexlock]]##
- ##[[KeyPgMutexUnlock Mutexunlock]]##
- ##[[KeyPgMutexDestroy Mutexdestroy]]##


Revision [15556]

Edited on 2011-12-15 09:50:12 by CountingPine [Reformat and mention threaddetach]
Additions:
- Threading is not allowed in the //[[CompilerOptlang -lang qb]]// dialect.
Deletions:
- Threading is not allowed in ##-lang qb##


Revision [15298]

Edited on 2011-10-01 07:12:12 by DkLwikki [Point to KeyPgThreadCreate instead of duplicating]
Additions:
See the ##[[KeyPgThreadCreate Threadcreate]]## examples.
Deletions:
See the [[KeyPgThreadCreate Threadcreate]] examples.


Revision [15296]

Edited on 2011-10-01 07:10:47 by DkLwikki [Point to KeyPgThreadCreate instead of duplicating]
Additions:
To avoid simultaneous access to shared resources from different threads, ""FreeBASIC"" implements mutexes, mutual exclusion locks that can be "owned" by a single thread when doing critical work. See ##[[KeyPgMutexCreate Mutexcreate]]##, ##[[KeyPgMutexLock Mutexlock]]##, ##[[KeyPgMutexUnlock Mutexunlock]]##, ##[[KeyPgMutexDestroy Mutexdestroy]]##
- **Threadwait** is not available with the DOS version of ""FreeBASIC"", because multithreading is not supported by DOS kernel nor the used extender.
- In Linux the threads are always started in the order they are created, this can't be assumed in ""Win32"". It's an OS, not a ""FreeBASIC"" issue.
- New to ""FreeBASIC""
Deletions:
To avoid simultaneous access to shared resources from different threads, FreeBASIC implements mutexes, mutual exclusion locks that can be "owned" by a single thread when doing critical work. See ##[[KeyPgMutexCreate Mutexcreate]]##, ##[[KeyPgMutexLock Mutexlock]]##, ##[[KeyPgMutexUnlock Mutexunlock]]##, ##[[KeyPgMutexDestroy Mutexdestroy]]##
- **Threadwait** is not available with the DOS version / target of FreeBASIC, because multithreading is not supported by DOS kernel nor the used extender.
- In Linux the threads are always started in the order they are created, this can't be assumed in Win32. It's an OS, not a FreeBASIC issue.
- New to Freebasic


Revision [15284]

Edited on 2011-10-01 07:02:24 by DkLwikki [Point to KeyPgThreadCreate instead of duplicating]
Additions:
See the [[KeyPgThreadCreate Threadcreate]] examples.
Deletions:
{{fbdoc item="filename" value="examples/manual/threads/threadwait.bas"}}%%(freebasic)
dim shared printsync as any ptr
sub mythread(byval idp as any ptr)
var id = cint(idp)
dim as double t, w
dim as integer i, n
if( id = 1 ) then
w = 1
n = 10
else
w = 0.3
n = 5
end if
for i = 1 to n
mutexlock printsync
print "Thread #"; id; ": on step #"; i
mutexunlock printsync
'' simulate some work
t = timer
while( timer - t ) < w
wend
next i
mutexlock printsync
print "Thread #"; id; " is done "
mutexunlock printsync
end sub
dim as any ptr t1, t2
print "Starting threads ... "
'' create a mutex to sync printing
printsync = MutexCreate()
'' create 2 threads, each taking a different
'' amount of time to complete
t1 = threadcreate( @mythread, cast(any ptr, 1) )
t2 = threadcreate( @mythread, cast(any ptr, 2) )
'' wait for threads to complete
threadwait( t1 )
threadwait( t2 )
mutexdestroy printsync
print "All done."
%%


Revision [11698]

The oldest known version of this page was created on 2007-11-10 11:06:09 by JeffMarshall [Point to KeyPgThreadCreate instead of duplicating]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki



sf.net phatcode