Wefts++ documentation.

0.99c

Author:
Giancarlo Niccolai
Release notes

Introduction

Wefts++ is an high-level abstraction C++ threading library, which has also seamless integration with low level constructs when raw speed is needed.

Wefts++ does not pretends to be a bleeding edge research library; what Wefts++ wants to be is just a set of utility objects making multi-threading with C++ easier, that will lay gracefully on your C++ application design.

Wefts++ aims to help you develop C++ multi-threading application easily and swiftly. Thread programming is an art, and wefts is your paintbrush

Note:
NOTICE: Wefts++ is built as a multi-platform library. Anyhow, current version only supports pthreads or windows threading. A correct installation of pthread library is essential to use Wefts++ on non-windows platforms. Windows user may chose to use Windows native threading or pthread for win32; currently, wefts implementation of Windows native threading is faster and more direct.

Obtaining Wefts++

Wefts++ library can be obtained in form of tar gzipped sources, in form of RPM source package or pre-compiled RPM binaries. Windows precompiled LIB files are also available. Installation is easy and it will just install a set of headers and a couple of very small libraries.

It is also possible to get latest development status of Wefts by downloading the CVS archive:

cvs -z9 -d:pserver:anonymous@cvs.sourceforge.net:/cvsroot/wefts login Password: <press enter here> cvs -z9 -d:pserver:anonymous@cvs.sourceforge.net:/cvsroot/wefts co wefts

Building from source tarball

Under GNU compliant systems, (and also under a lot of Unixes) a
./configure make make install (** read note)

should be enough. Wefts are installed by default into /usr/local/lib/wefts. That directory will contain an include/ subdirectory, where all the include files are added and lib/ subdirectory, where you can found static (and if you care to build them, dynamic) libraries.

You can change this default with the option:

./configure --prefix=/my/favorite/

You could i.e. install wefts libs under /usr/libs and include files under /usr/include by just configuring with --prefix=/usr (if you find this library really useful :-). You could also set specific destination for wefts headers and libraries. In example:

./configure --prefix=/usr --libdir=lib/wefts --includedir=include/wefts

will move libraries and includes in your system standard directory, but under wefts subdirectory (/usr/include/wefts for headers and /usr/lib/wefts for libraries).

Note:
if you install wefts in a standard directory, using i.e. --prefix=/usr, you will be able to use include files, static and especially dynamic libraries without any extra compilation/link flag.
A script named "wefts-conf" will be installed in your /usr/local/bin dir; it is a small script that you can later on use in your makefiles to automatically add the compilation and library flags needed to link against wefts. If you don't like this, you can call ./configure --bindir=... to set a different relative or absolute path. A relative path will be added below --prefix directory.

Installing from CVS

Provided you have autoconf 2.5+, automake 1.6+ and a recent libtool, this command sequence should be enough:

aclocal libtoolize --automake autoconf autoheader automake -a

And then you can continue with configure, make and make install. "automake -a" is important, as it will create the standard GNU support utilities to do a clean configure/make process.

Other configure options

To enable debugging symbols add --enable-debug, and to make also tests (linked against static library) do --enable-tests.

Dynamic libraries can be built by passing --enabled-shared to configure. Anyhow, wefts is a very small library, and most of it is held in inline C++ methods; for this reason, using only the static library version makes perfectly sense.

Rpm installation

Wefts can also be installed from RPM on Mandrake and Red Hat LINUX systems, and on those systems that support RPM. The library installation is done under /usr/lib, include files are installed in /usr/include/wefts, while wefts-conf script is placed in /usr/bin dir.

To install the package, you need libpthread.a to be in /usr/lib.

The command rpm -ivh wefts-...rpm will be enough.

Unistalling

Once you have configured and installed the package, if you don't like the libraries anymore ;,( a make uninstall will remove installed files.

If you have installed using rpm, the command rpm -e wefts will remove the package.

Getting Started

This manual is meant to be just an API reference (and a configuration guide) for Wefts; cute tutorials, FAQ, and other things like that can be found at wefts home site http://wefts.sourceforge.net.

However, we present here a minimal survival guide on how to compile and use wefts.

To test if your installation is correct, check out this test:

// $Id: weftstest.cpp,v 1.1 2003/08/16 20:45:57 jonnymind Exp $ // weftstest.cpp - an introductory test // // This program is free software - GPL 2.0 license // (C) Giancarlo Niccolai // // NOTE: this program is demonstrative and hence not compiled // in the make process of Wefts test suite. // #include <wefts.h> #include <iostream> using namespace Wefts; using namespace std; class MyThread: public Thread { void *run() { cout << "Begin of thread" << sequence() -1 << endl; Sleep( 2.5 ); cout << "End of thread" << sequence() -1 << endl; return 0; } }; int main( int argc, char *argv[] ) { for ( int i = 0;i < 4; i ++ ) { MyThread *th = new MyThread(); th->start(); } //we'll wait for our children to terminate: runningThreads.until( 1 ); // until there is only 1 thread left (myself). cout << "Main thread done!" << endl; return 0; }
00001 // $Id: weftstest.cpp,v 1.1 2003/08/16 20:45:57 jonnymind Exp $ 00002 // weftstest.cpp - an introductory test 00003 // 00004 // This program is free software - GPL 2.0 license 00005 // (C) Giancarlo Niccolai 00006 // 00007 // NOTE: this program is demonstrative and hence not compiled 00008 // in the make process of Wefts test suite. 00009 // 00010 00011 #include <wefts.h> 00012 #include <iostream> 00013 00014 using namespace Wefts; 00015 using namespace std; 00016 00017 class MyThread: public Thread 00018 { 00019 void *run() 00020 { 00021 cout << "Begin of thread" << sequence() -1 << endl; 00022 Sleep( 2.5 ); 00023 cout << "End of thread" << sequence() -1 << endl; 00024 return 0; 00025 } 00026 }; 00027 00028 int main( int argc, char *argv[] ) 00029 { 00030 for ( int i = 0;i < 4; i ++ ) { 00031 MyThread *th = new MyThread(); 00032 th->start(); 00033 } 00034 00035 //we'll wait for our children to terminate: 00036 runningThreads.until( 1 ); // until there is only 1 thread left (myself). 00037 00038 cout << "Main thread done!" << endl; 00039 return 0; 00040 } 00041

This simple program can be compiled using the command

$ gcc -o weftstest $(wefts-conf -c) weftstest.cpp $(wefts-conf -s -l)

This command will call wefts-conf two times; the first one will provide some basic C++ flag, including the "include" dir that is needed to do #include <wefts.h>. The second one will tell where to get the wefts library that has to be linked.

If you don't have installed dynamic libraries, (that is, if you don't have enabled them with --enable-shared in configure script), that "-s" option in wefts-conf isn't necessary. In fact, that option just tells to get exactly the static library. Wefts is a very small library (under 50 Kb), so it is possible that the final developer does not want to link against dynamic libraries, with the need to install them everywhere the program has to run.

Installing and using dynamic libraries

Dynamic libraries are a little more tricker. By default they are installed in /usr/local/lib/wefts/lib; this means that you need to do something to tell the program loader where the libraries are, to use them. You can do three ways:

Note:
In RPM installation, the default library directory is /usr/lib, so the "error while loading shared libraries" is bypassed; anyway, it is still worth to link Wefts++ statically so the resulting program can be moved around without having to install the library.

Enough! I want to start thread programming!

Ok, Ok, I feel that way too :-)

So, what can you do now? The main class here is Wefts::Thread; have a look at it. Then you will need some way to synchronize threads: Wefts::Mutex and Wefts::RMutex are what you are searching for.

If you are accustomed with posix condition and signals, the Wefts::Condition, Wefts::FastCondition and Wefts::RCondition classes are implementing that model. If you are not accustomed with them, or if you have enough of condition signaling, well, have a look at them anyway; you could find new way to use them in this C++ object oriented environment. They are absolutely excellent to send safely a data that is produced in a thread to another thread that is waiting for that. (If you use them directly, and if you plan to use thread cancellation, be sure to read Condition cleanup)

A little step further is required for Wefts::XMutex, Wefts::RWMutex and Wefts::RRWMutex.

Then, when you have a certain mastery with this classes, have a look at the Wefts::Barrier, Wefts::RingBuffer, the Wefts::Subscription and finally the Wefts::MoaningThread (TM). -- this (TM) is a joke.

But I would like to see some sample...

Almost all features are demonstrated by little programs in the tests/ subdirectory of the source distribution (both SRPM and tarball has it, or you can browse our CVS). They will be compiled (from tarball) if you set ./configure --enable-tests.

They are somehow self explanatory, and are worth a look anyway.

Each one is meant to do some MESSY test with the library thing. You are supposed to read the header that is displayed at program begin; if it goes away too fast, it is because you are supposed to read it by scrolling back and then see the results of the experiments with caml, after the program is finished; you may also want to to dump the results by redirecting them to a file. In the test sources there are LOTS of comments explaining why certain things are done in certain ways; if you want to learn Wefting, that is a fairly good point to start.

Ok, all this is nice, but I need some tutorial

Sorry. Not here. Try to see http://wefts.sf.net to see if me (or another contributor) had the time to add some. Or scan the net to see how other programmers have solved the problem you have. And if you are lost, send me a mail. (My address is about everywhere in the distribution...)

But ... to be honest in release 0.9 there is a fairly good section explaining how to use basic conditions and mutexes; big issues (like the most powerful objects) are left out, but you may want to check Some example.

Enjoy Wefting!

Giancarlo Niccolai


Generated on Tue Oct 5 14:57:00 2004 for Wefts by doxygen 1.3.7