Skip to content

Multi-Threading


The Multi-threading programming by using the built-in support provided by FreeBASIC.

Preamble:

Multi-threading programming allows to spawn concurrent process flow.

It is most effective on multi-processor or multi-core systems where the process flow can be scheduled to run on another processor/core thus gaining speed through parallel or distributed processing.

While most effective on a multi-processor or multi-core system, gains are also found on single-processor or single-core systems which exploit latency in I/O and other system functions that may halt process execution.

One thread may execute while another is waiting for I/O or some other system latency.

Threads require less overhead than spawning a new process because the system does not initialize a new system virtual memory space and environment for the process.

All threads within a process share the same address space.

Definitions

A multi-threaded process contains two or more parts that can run concurrently.

Each part of such a program is called a thread, and each thread defines a separate path of execution.

Difference between a process and a thread:

  • Process:
  • Thread:

Threads are the popular way to improve program through parallelism.

Multi-threaded programming

Single-threaded program executes one line of code at a time, then move onto the next line in sequential order (except for branches, function calls etc.).

This is generally the default behavior when programmer writes code.

There are a few main reasons to create threads, for example:

  • You need to perform a task which takes a long time to complete but don’t want to make the user wait for it to finish. This is called task parallelisms. The purpose of creating threads is to ensure the application maintains responsiveness in the user interface, rather than to actually make the task run faster. Importantly, the task must be able to run largely independently of the implicit main thread for this design pattern to be useful.

  • You have a complex task which can gain a performance advantage by being split up into chunks. Here you create several threads, each dedicated to one piece of the task. When all the pieces have completed, the main thread aggregates the sub-results into a final result. This pattern is called the parallel aggregation pattern. For this to work, portions of the total result of the task or calculation must be able to be calculated independently – the second part of the result cannot rely on the first part etc.

Multi-threaded program is executing from two or more locations in the program at the same time (or at least, with the illusion of running at the same time):

  • Sequencing overview:
  • Threading on multi-processor or multi-core systems:
  • Thread safety:
  • Synchronization objects:

Multi-threading is quite dangerous, the mistakes here are very costly. When a problem is found, it’s either hard or impossible to debug, but it’s not the worst.

The worst is when the code has a mistake, but it works correctly.

This can happen because once threads are used, the execution flow is not deterministic anymore.

How long does it take to create a new thread?

How many concurrent threads can there be?

How is the CPU time distributed among the threads?

It is advised to always set a 'Sleep x, 1' tempo in each 'For' loop of each thread (including the main thread), in order to release time-slice allowing the other threads to execute as well.

All these factors affect the overall execution. Writing multi-threaded code is about being prepared to anything, and that’s making it both dangerous and exciting.

Multi-threaded built-in support

The thread-safe runtime library is automatically used if the FreeBASIC built-in threading functions are used (so the '-mt' compiler option for the linker, is only needed if programmer wants to use his own threading routines).

The following pages of this section ("Multi-Threading") explain:

See also

Back to DocToc

Translated from FreeBASIC official docs. Contact us for removal if infringed.
FreeBASIC is an open-source project, not affiliated with Microsoft