DEV Community

ivinieon
ivinieon

Posted on

How to decide the number of threads based on CPU bound/ IO bound

How to decide the number of threads based on CPU bound/ IO bound

Review/Preview words

CPU(Central Processing Unit) is the device that interprets and executes the commands of a process.

IO(Input/Output) involves reading and writing files, sending and receiving data with a network, and interacting with input/output devices.

Burst is a phenomenon that occurs intensively within a short time.

CPU burst refers to the time a process is continuously executed on the CPU.

IO burst refers to the time a process waits for an IO operation to complete after requesting it.

The life of a process is a continuous sequence of CPU and IO bursts.

The frequency of CPU bursts depends on their length.

Image description

CPU bound process
A process that has many CPU bursts.
ex) video editing software, machine learning programs

IO bound process
A process that has many IO bursts.
ex) typical backend API servers

If implementing a CPU bound program to run on a dual-core CPU, how many threads should be used?

→ The number of threads = The number of CPUs + 1
Image description

In actual execution:
Image description
→ Context switching(CS) is overhead, tasks that use the CPU.
In this picture, unnecessary context-switching occurs, the CPU usage increases.

Dual-core & 2 threads:
Image description
Since there is no context switching, CPU usage is not wasted, and the appropriate number of threads is one more than the number of CPU cores.

If implementing an IO bound program to run on a dual-core CPU, how many threads should be used?

→ It depends on the situation.

  • If the API server is a thread-per-request model? → The number of threads to be created in advance should be determined by considering various situations.

This posting is just a study note which was written after watching youtube videos in Korean.
Youtube link

Top comments (0)