#What're OS thread and CPU thread?

24 messages · Page 1 of 1 (latest)

blissful oriole
#

So I'm trying to learn about thread, i've tried to create 8 threads in my project and it consumed 100% of my CPU, but some people say that i can have THOUNDSAND OF THREADS, maybe OS thread, so how can i create OS thread?

coral hemlockBOT
#

Hey, @blissful oriole!
Please remember to /close this post once your question has been answered!

latent pebble
#

Yeah, no. You can indeed have thousands of them, but if they're not designed to sleep most of the time (like most processes of an OS do) then that will be very inefficient

#

Indeed all processes of an OS use at least one OS thread so yeah it's designed so that you can run thousands of them in the same system all right. It's not designed so that they play well together if they all want the CPU

true charm
#

having to call new Thread is a very rare condition

latent pebble
#

..... If they can use 100% CPU they'll still take 100% CPU

true charm
latent pebble
#

I mean, I don't typically write tasks that can keep taking up 100% CPU. But if I did, that's what it's supposed to do unless you want them managed to take up less

#

(Yes, I work with cryptos, but they typically use a weird CPU/GPU combine where what's loaded is the GPU. And they don't use threads per se as the tasks are nonblocking as long as they're not solved)

blissful oriole
#

But i'm kinda confuse about OS thread and CPU thread

#

I've created 8 threads that run a while loop and it used 100% CPU

#

But what is it mean to have thousand of os threads?

blissful oriole
latent pebble
#

Well yeah, as long as that's at least as many CPU cores as you have, that's what they're supposed to do

#

Or would you rather your computer to slack off on you?

blissful oriole
#

Look at this, how can i create 2000 threads?

latent pebble
#

They mention a theoretical max number that can be created. They didn't say it would be smart to create that many

#

Still, if your threads can possibly wait on some locks, then they'll stop hogging the CPU for as long as they don't get the lock. That's what most programs constantly do: waiting, and why an OS can have tons of threads at the same time

true charm
#

This is really an electrical engineer problem. As a developer we mainly only deal with the cores on the cpu and memory management. As far as thousands of threads, youd be lucky to have 100 threads that are actually concurrent and not a millisecond apart