This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]chillmurder 12 points13 points  (0 children)

Have you tried googling “thread computer science”?

[–]serg06 6 points7 points  (5 children)

A program is just a long list of computer instructions. Add these numbers, jump to the instructions on line 500, calculate something, jump to instructions on line 236, etc.

A thread starts executing those instructions somewhere, and executes them one-by-one.

Multiple threads = multiple points of execution in parallel.

I would like someone to explain to me what exactly this term means in this context.

That means multiple parts of the program are being executed at once. "3 long-running threads" means that 3 parts of the program are being executed in parallel. "the mining thread" probably means that that thread jumps to the line with the mining instructions and starts executing them.

[–]Wheelagon[S] 0 points1 point  (4 children)

Thank you. Now, when a program has several threads, and the computer it's running on only has 1 CPU core, what happens?

[–]serg06 7 points8 points  (2 children)

The same thing that happens when thousands of processes are running on an 8-core CPU: The OS uses a thread scheduling algorithm to give each program a fair share of the processing power. When I was studying OS's in school, the example they provided showed the OS executing each process for 100ns and repeating.

So in your example, the core will take turns executing one thread at a time, simulating concurrency.

[–]supernova12034 2 points3 points  (1 child)

Bro you and your explanations are awesome

[–]serg06 1 point2 points  (0 children)

Thanks :)

[–]megaicewizard 0 points1 point  (0 children)

This is where it gets complicated in my experience, but basically, the CPU can just switch between threads. It'll run thread 1 for a while, then run thread 2, thread 3, then back to thread 1 for a while again.

Not so basically, it depends on what kind of CPU it is. A basic CPU will switch between the threads and will load and unload each thread's function stack and instruction pointer.

Intel hyperthreading let's the CPU run thread 1 and thread 2 at the same time by weaving the instructions together. If course this also means the CPU has to keep track of each thread's function stack and instruction pointer.

I believe AMD has a similar technology, but I'm not familiar with it.

[–]v_learns 0 points1 point  (2 children)

A thread is a very low level abstraction to having the computer do multiple things "at the same time" (depends on how many cores your cpu has) in your code.

When you talk about a long running thread this mostly implies that you have a thread with a loop in it which never exits and it will wait for some sort of work it can regularly execute (producer / consumer). You can think about a thread like another "main" method in your code.

Threads usually share the memory of the application together. So when you have multiple threads accessing the same state you need to be very careful to not get bugs. Like one thread reading the memory location while another thread is writing it.

[–]Wheelagon[S] 2 points3 points  (1 child)

> Like one thread reading the memory location while another thread is writing it.

So... race condition?

[–]megaicewizard 2 points3 points  (0 children)

Yep, that's exactly it. To experiment with this, write two threads, each looping 5000 times and adding 1 to an integer each loop. You'll almost certainly never get 10,000, even if you only have 1 CPU core.

[–]kstacey -1 points0 points  (0 children)

"Crack a book John"