Markus Gerwinski proudly presents:
GNU Application Libraries for Pascal, in short GNUALP, pre-release 0.1.4.
The current status of the package contains units for - exception handling, - standard collection types (lists, hashtables etc.), - system time and calendar functions, - and last but not least: multithreading.
You can download a tarball with the current sources from
http://www.markus.gerwinski.de/gnualp/gnualp-0.1.4.tar.gz.
The library is tested on SuSE Linux 8.0, 9.1, and Windows XP with MinGW.
You can install the tarball just the usual way, i.e. unpack it into your source directory, then run "./configure", "make", "make install", and everything will be in place. The API units will then be installed to a directory named /usr/local/units/gnualp-0.1. This also generates a shared library, libgnualp.so, which you'll have to link against. E.g. to compile a program foo.pas which uses the GNUALP library units, your gpc compiler call will have to look like this:
gpc --automake --unit-path=/usr/local/units/gnualp-0.1 foo.pas -o foo -lgnualp
This library is work under development and far from being complete, so it is all undocumented up to now. However, since several folks seemed to have a pressing urge for multithreading, I'll give a short introduction here on how to use the resp. features of the library:
- Include `AlpThreads' into the uses clause of your program. Declare any threads you want to start as vars of type `pThread'.
- To run some simple procedure as a thread, give it a parameter list like this:
Procedure doingSomething ( thread: pThread; data: pointer );
You can then from anywhere in the program create a thread object for this procedure with the following statement:
someThread:= new ( pSimpleThread, start ( doingSomething, nil ) );
The thread then starts running immediately and will automatically dispose itself after it is ready. It can be stopped from within the doingSomething procedure using the command `thread^.stop'. In this case, it will also dispose itself.
For more sophisticated thread handling, derive an object type from the abstract tThread type and override the `work' method to do whatever the thread is supposed to do; if need be, you can also override the `cleanup' method. This one's always executed at the end of the thread, no matter if it was regularly ended or `stop'ped in-between.
- Mutexes and semaphores are represented by the tMutex and tSemaphore object types. To create e.g. a mutex, just call
mutex:= new ( pMutex, init );
To lock the mutex, call `mutex^.lock'; to unlock it, call `mutex^.unlock'. To dispose it after use, call `dispose ( mutex, fini )'. Just as simple. :-) Semaphores work in an analogous way; they only require an integer parameter in their constructor to determine the maximum count of threads they may be accessed by.
For usage examples, see the files simplethreads.pas, mutexes.pas, and semaphores.pas in the demos/pas subdirectory of the gnualp package. They aren't documented or commented either, since I've only set them up as a test for myself, but I hope the code isn't too difficult to read. :-}
Regards,
Markus