CITS2002 Systems Programming  
prev
next CITS2002 CITS2002 schedule  

Some definitions

Threads are a programming abstraction designed to allow programmers to control concurrency and asynchrony within a program.

In some programming languages, like Java, threads are "first-class citizens" - they are part of the language specification itself. For others languages, like C and C++, threads have not traditionally been part of the language specification, and were implemented as a library of functions linked with, and called by, a program.

This changed with C11 which now defines portable threads as part of its standard.

The differences between having threads "in the language" and threads "as a library" are subtle and important. For example, a C compiler need not take into account thread control (leaving that to the programmer), while a Java compiler must. Analogously, we see that C enables fine-grain control of memory mangement, while Java strictly defines it as part of its standard.

In the library case, it is possible to choose from different thread libraries to provide different semantics. In contrast, (all conforming) Java and C11 programs are required to support their single thread model and API.

What is a thread?

We'll informally define threads within a program to include three things:

  • a sequence of instructions to be executed,
  • a set of local variables that "belong" to the thread, and
  • a set of shared global variables that all threads can read and write.

This definition corresponds roughly to the C language model of sequential execution and variable scoping. Operating systems are still significantly written in C and, thus, thread libraries for C are easiest to understand and implement when they conform to a familiar model.

And why?

Threads provide two important opportunities:

  • they allow us to deal with asynchronous events synchronously and efficiently, and
  • they allow us to get parallel performance on a shared-memory multiprocessor.

 


CITS2002 Systems Programming, Lecture 20, p2, 9th October 2023.