Chapter 10. Threading

This chapter will go over the writing of multi-threaded applications in Hassium. A multi-threaded application refers to one in which there are two or more threads executing on the CPU at the same time. This chapter will cover declaring a thread, thread management, returning from threads, and the thread do statement.

10.1 - Declaring a Thread

A thread object can be declared using the thread keyword, followed by the body of the thread.

func main () {
    th = thread {
        while (true) {
            println ("This is being displayed from a thread.");
        }
    }
}

Note: This code will not do anything since the thread has not been started. 10.2 will cover starting and stopping threads.

The body of a thread has access to the locals of the parent defining it, even when the thread is returned. The following code declares a function, which returns a thread counting to the two parameters given to the function.

func largecount (x : int, y : int) : thread {
    return thread {
        while (x < y) {
            println (x++);
        }
    }
}

10.2 - Thread Management

The three main ways to manage a thread are the thread.start, thread.stop, and sleep functions.

10.2.1 Threads - Starting

Let's define a thread that infinitely displays the string "Hello from a thread" and start it next to the main thread.

func main () {
    th = thread {
        while (true) {
            println ("Hello from a thread");
        }
    }
    th.start ();

    while (true) {
        println ("Hello from the main thread");
    }
}

Output:

Hello from a thread
Hello from the main thread
Hello from a thread
Hello from the main thread
Hello from a thread
Hello from the main thread
...

Since the body of th is executing on a separate thread, the code in main executes at the same time as th.

10.2.3 Threads - Stopping

We can stop a thread using thread.stop. Let's make a program which executes th until the main thread can count to 100, 000.

func main () {
    th = thread {
        while (true) {
            println ("Hello from a thread");
        }
    }
    th.start ();

    for (int i = 0; i < 100000; i++) ; # count to 100, 000
    th.stop ();
}

This should only output Hello from a thread for less than one second. Then the loop in main will finish and execute thread.stop.

10.2.4 Threads - Sleeping

A thread can be temporarily suspended for a specified amount of milliseconds by calling the global sleep function from inside that thread. Test sleep in the main thread using the code below.

func main () {
    println ("Wait...");
    sleep (3000); # hold 3 seconds.
    println ("Just wait...");
    sleep (4000); # hold 4 seconds.
    println ("Almost there...");
    sleep (3000); # hold 3 seconds.
    println ("DONE!");
}

Each call to sleep will suspend the main thread for the given amount of milliseconds.

10.3 - Returning from Threads

A thread can act like a function and return a value. When a thread returns a value, it is sent to the thread.returns property. The caller is aware when the thread has returned using the thread.isalive property.

Take a look at this code which defines a thread which will count to 100, 000 before returning a string. The main thread must wait until the thread has exited to access the return value.

func main () {
    th = thread {
        for (int i = 0; i < 100000; i++) ;
        return "Done!";
    }
    th.start ();

    while (th.isalive) sleep (2); # keep hanging until the thread is finished.

    printf ("The return value is {0}.", th.returns);
}

10.4 - The thread do Statement

In all of the previous examples, for a thread to be run it must first be declared, coupled with a call to thread.start. Using the thread do statement, a block of code can simply be executed in a new thread with no hassle.

func main () {
    thread do {
        while (true) {
            println ("One");
        }
    }
    thread do {
        while (true) {
            println ("Two");
        }
    }
}

Chapter 10 - Exercises

Exercise 10.1

Create a program which spawns three threads that all race to count to 1, 000, 000. Display which thread reached the number first. Once one of the threads has the correct value, all of the other threads should be stopped.

Exercise 10.2

Create a function that takes in a message and returns a new thread that infinitely displays that message.

Exercise 10.3

Create a thread that counts up from 0 infinitely. Wait seven seconds before stopping the thread and displaying the value the thread reached in those seven seconds. Hint: You will have to declare the counter variable in a higher scope than the thread declaration.

results matching ""

    No results matching ""