Using HybridthreadsBecause the Hthreads API is designed to be POSIX Threads compatible programming for the Hthreads system is much like programming with the pthreads library. Just like the pthreads library, programs written in the Hthreads system consists of multiple threads of control running in the same address space and controlled through syncrhonization primitives. Creating Software ThreadsThe creation of software threads in the Hthreads system is almost exactly like the creation of threads in the pthreads library, and it is often very easy to convert existing pthreads programs into software only Hthreads programs.
InitializationThe first step in the creation of any Hthreads program is to include the top level Hthreads header file which provide the definitions of all of the functions available to Hthreads programs. Once this header file has been included the Hthreads program that you are writing will have access to all of the Hthreads functionality. An example of this is shown below. // Include the Hybridthread header file so that // Hybridthread functions are properly defined #include
int main(int argc, char *argv[]) { // All of the functionality in the Hthreads // system is available for use Creating More ThreadsOften the first thing done in a multithreaded program is to create a set of children threads to run. In the Hthreads system this is done with calls to the hthread_create function. This function take four parameters; two are optional and two are required.
- A Pointer to Thread ID Structure (Required)
- A Pointer to Thread Attribute Structure (Optional)
- A Pointer to Start Function (Required)
- A Pointer to Arbitrary Paramter (Optional)
An example of a Hthreads program which creates several children threads is shown below. // Include the Hybridthread header file so that // Hybridthread functions are properly defined #include
void* hello_thread( void *arg ) { printf( "Hello, World\n" ); return NULL; }
int main(int argc, char *argv[]) { // Declare a thread id structure which will hold the // thread id created by the call to hthread_create hthread_t tid;
// Initialize the Hybridthreads System. // This should be the first function call in main hthread_init();
// Create the hello world thread. The thread is // automatically scheduled to run when it is first created hthread_create( &tid, NULL, hello_thread, NULL );
// Wait for the newly created thread to run and exit. // This is required if the program is going to run correctly. hthread_join( &tid, NULL );
// Return status of the program return 0; }
|