GNU Astronomy Utilities

Next: , Previous: , Up: Gnuastro library   [Contents][Index]

10.3.2 Multithreaded programming (threads.h)

In recent years, newer CPUs don’t have significantly higher frequencies any more. However, CPUs are being manufactured with more cores, enabling more than one operation (thread) at each instant. This can be very useful to speed up many aspects of processing and in particular image processing.

Most of the programs in Gnuastro utilize multi-threaded programming for the CPU intensive processing steps. This can potentially lead to a significant decrease in the running time of a program, see A note on threads. In terms of reading the code, you don’t need to know anything about multi-threaded programming. You can simply follow the case where only one thread is to be used. In these cases, threads are not used and can be completely ignored.

When the C language was defined (the K&R’s book was written), using threads was not common, so C’s threading capabilities aren’t introduced there. Gnuastro uses POSIX threads for multi-threaded programming, defined in the pthread.h system wide header. There are various resources for learning to use POSIX threads. An excellent tutorial is provided by the Lawrence Livermore National Laboratory, with abundant figures to better understand the concepts, it is a very good start. The book ‘Advanced programming in the Unix environment’130, by Richard Stevens and Stephen Rago, Addison-Wesley, 2013 (Third edition) also has two chapters explaining the POSIX thread constructs which can be very helpful.

An alternative to POSIX threads was OpenMP, but POSIX threads are low level, allowing much more control, while being easier to understand, see Why C programming language?. All the situations where threads are used in Gnuastro currently are completely independent with no need of coordination between the threads. Such problems are known as “embarrassingly parallel” problems. They are some of the simplest problems to solve with threads and are also the ones that benefit most from them, see the LLNL introduction131.

One very useful POSIX thread concept is pthread_barrier. Unfortunately, it is only an optional feature in the POSIX standard, so some operating systems don’t include it. Therefore in Implementation of pthread_barrier, we introduce our own implementation. This is a rather technical section only necessary for more technical readers and you can safely ignore it. Following that, we describe the helper functions in this header that can greatly simplify writing a multi-threaded program, see Gnuastro’s thread related functions for more.



Don’t let the title scare you! The two chapters on Multi-threaded programming are very self-sufficient and don’t need any more knowledge than K&R.


Next: , Previous: , Up: Gnuastro library   [Contents][Index]