Savoga

Threads


Bounds

To know if multithreading can speed up a task, one should know the types of bounds that are saturated:

  • CPU-bound (e.g. calculation) -> Python: multithreading doesn’t work (because of the GIL), multiprocessing is the workaround / C++: multithreading works

  • I/O-bound (e.g. accessing files using an external system) -> concurrency (async or multithreading) helps

  • memory-bound (RAM, e.g. copying data)

  • other types of bounds (less common)

Start

Use std::thread t1(do_something);

.detach(): let run the thread in the background independently. Ideally stated right after starting the thread.

Finish

.join(): used to wait for a thread to finish. Note: we say “join” because the thread finishes in joining others e.g. it finishes in joining the main thread (example below).


int main() { 
	std::thread t(do_something);
	t.join();
}