What is a thread in a kotlin coroutine? - multithreading

I have read many questions here and several books on kotlin coroutines, but none answers the question of what a thread is.
For example, I run an app on my huawei gr5, which has 4 cores. But people are talking about spawning 128 threads on the dispatcher.IO.
Is this thread the same as the logical thread when we say an Intel i5 cpu has 6 cores and 12 threads or is it just a concept, a set of CPU instructions?
Thank you.

Related

What is a difference between CPU threads and program threads

For example i5 7600k has 4 threads, but game can have more than 4 threads. What is the difference and why they have the same name?
A CPU that has 4 threads (really a CPU with 4 cores, or possibly a 2 core CPU with Hyperthreading) can execute 4 separate threads simultaneously. A program can have more threads than that, but only 4 of them can be executing at any given time - the others would be in a sleep/wait state while they wait for the CPU to become available.
As for how the CPU "becomes available" for other threads when there are more threads than it can execute at a given time, that's a function of the operating system scheduler. The operating system scheduler rotates threads on and off the CPU periodically (typically every few milliseconds) so that every thread that wants to execute eventually gets its turn on the CPU.
There's more to it than that, but hopefully that covers the gist of your question.

Maximum number of threads CPU

What does this have to do with the threads of the processor? for example, an Intel i5 has four cores and four threads.
How many threads can we use in our program? for example using std :: thread (STL) in C ++?
is 8 threads a big or low number of threads for a program?
It really depends. As a rule of thumb, limit the number of threads to something close to the number of cores (otherwise you might have too much context switchs). You might use std::thread::hardware_concurrency() as a hint. Often, you organize your program with a thread pool.
However, what really matters is the number of active threads. Some programs have hundreds of threads but are organized so that only a few of them are active (i.e. runnable) at any given instant, and most of them being idle (waiting for IO, or for some mutex or condition variable).
Be aware that a thread is a quite heavy resource (in particular because it has its own call stack, usually at least a megabyte). So don't have too many of them (hence, having thousands of threads is generally unreasonable, unless you have a very powerful and expansive computer).
The Intel hyper-threading technology is often disappointing. You probably don't want to have 8 active threads on an Intel processor with 4 cores and 8 hyperthreads. You need to benchmark, but you should expect some bad performance in such case (perhaps having 4 or 6 active threads would make your overall performance better).
Threads are an abstraction provided and managed by the operating system. So read Operating Systems: Three Easy Pieces to understand more about OSes.

What is the difference between the thread in CPU (hardware) and those in thread pool

People always call CPU has 4 cores & 8 threads or 2 cores & 2 threads, etc.
But in the thread pool, there are quite a lot of tiny threads generated, are these related to the hardware threads?
I am thinking if the CPU threads are actually processes.
Also, I think the actual thread is just a block of code that runs while loop and execute available task otherwise sleep, is this statement correct?
On the hardware side the CPU has cores, cores have 1-8 schedulable threads, new Power-CPUs have up to 8 threads, Knightbridge(?) has 4, most other desktop CPU's have 2, older and/or smaller CPU's got 1.
On the software side a program can have multiple processes (different virtual memory mapping), a process can multiple software threads (sharing the processes memory map), a software thread is the scheduling partner for the hardware thread.
Then you again can have a logical thread in software, often called a fiber, which is a user software scheduled mini thread run by a software thread.

Determinated threads running with X cores?

If I have 8 asynchronous functions to execute and 4 cores in my CPU, What is better (more speedy and correct)?
Start 4 threads and when finish a thread, start another thread, etc.
Start 8 threads
Language aren't important.
The answer is: It depends.
It depends on the workload. If threads are I/O bound, you can benefit by starting all 8 threads. If they're highly CPU bound, then it makes less sense.

POSIX Threads on a Multiprocessor System

I have written software which takes advantage of POSIX threads so that I can utilize shared memory within the process. My question is if I have a machine running Ubuntu with 4 processors and each processor has 16 cores. Is it more efficient to run 4 processes each with 16 threads or 1 process with 64 threads? Each processor has a dedicated 32gb of ram.
My main worry is that there will be a lot of memcopy happening behind the seen with 1 process.
In summary:
On a 4(16core) Proc Machine
1 process 64 threads? 4 Processes 16 Threads each?
If the process requires more than 32 gb of RAM(The amount dedicated to one Proc) does the answer differ?
Thanks for your help
Depends on what your application does.
A thread in a single-threaded process runs faster then a thread in a multi-threaded process since the latter requires synchronization between threads in library functions like malloc(), fprintf(), etc.. Also, more threads in a multi-threaded process are likely to cause more lock contention slowing down each other. If threads don't need to communicate and don't share data they don't need to be in the same process.
In your case, you may get better parallelism with 4 processes with 16 threads rather then 1 process with 64 threads.

Resources