|
 |
Online Resources |
 |
 |
Libraries |
 |
|
 |
 |
Thread |
 |
 |
Overview
A thread is a part of a program which runs asynchronously, in the background
of this program. This means than it's possible to perform long operations
(compression, image processing, etc) without halting the whole program and let the user
continue to do other things. A thread runs within your program, it's not
another process. When the main program exits, all the threads are destroyed.
Under PureBasic, threads are simply a procedure which is called asynchronously.
The thread runs until the procedure exits.
Examples of places of programs where threads are useful are when you need to be able to handle
multiple situations with different response times or which occur at different
intervals. In the above paragraph, the response times of image processing and the
user interface are quite different (you would want to wait for the image to be processed
but always have the user interface to respond).
Note: Threads need to be used carefully because it is possible that you can have
multiple access to shared resources (memory, variables, files, etc) and you need
to manually ensure that you do run into trouble because of this. For example, it
is not safe to modify or write to strings from more than one thread because strings
share the same internal memory buffer. If you only ever read from strings while
your threads are running then it should be safe.
If you still want to modify strings safely in threads (or any other shared access resource),
you need to make sure that only one thread can use strings at a time. To do this you
should make use of the synchronisation capabilities of the OS you are running on. It
is not safe to simply use a global variable as a flag to indicate that the string buffer
(or anything else) is being used. How can you ensure correct synchronisation when
modifying this global variable?
Note: Don't use DirectX inside threads (MS Windows limitation)! If you need to display
graphics in threads use Images and
2DDrawing instead.
Command Index
CreateThread
KillThread
PauseThread
ResumeThread
ThreadPriority
WaitThread
Example
Thread.pb
Supported OS Windows, Linux Reference Manual - Index
|
|
 |
|
|