about understanding Threads

T

Tony Johansson

Hi!

I have some questions about threads.

We can assume for simplicity that we have a computer with only 10 processes
with no attitional thread except the one that is already existing in a
process by default then each of these processes will be given an equal
timeslice to execute the program in.

Now we have the same computer with 10 processes but now one of these 10
processes have three additional threads + the default one so as a total of
four.
Question 1: So will this process having four Threads be give one time slice
to each of its Threads ??

Question 2: I have the understanding that if you have a computer with a
single process no more then one thread can be executing at any single time
so matter how many threads exist for this process.
Is that correct understood ?

//Tony
 
P

Peter Duniho

Tony said:
Hi!

I have some questions about threads.

We can assume for simplicity that we have a computer with only 10 processes
with no attitional thread except the one that is already existing in a
process by default then each of these processes will be given an equal
timeslice to execute the program in.

If every process is CPU bound — that is, all of its work require the CPU
and it doesn't ever do anything that would cause it to have to wait on
some other process or i/o — then you can make that assumption.

Of course, real life isn't anything close to being like that. But we
can start there.
Now we have the same computer with 10 processes but now one of these 10
processes have three additional threads + the default one so as a total of
four.
Question 1: So will this process having four Threads be give one time slice
to each of its Threads ??

In your very simplistic OS design scenario, yes. Not counting all the
other things that affect thread scheduling, threads are scheduled "round
robin", each given a fixed timeslice and will be permitted to run until
it's used that timeslice, at which time the thread will be pre-empted
and the next thread in the order will be allowed to run.
Question 2: I have the understanding that if you have a computer with a
single process no more then one thread can be executing at any single time
so matter how many threads exist for this process.
Is that correct understood ?

Incorrect. The thing that limits how many threads can run
simultaneously is the number of CPU cores that are present, not the
number of processes that are running.

In fact, if the OS only ever allowed one thread for each process to run
at a time, there would be very little incentive for anyone to bother
with writing a multi-threaded application. Multithreading can be a nice
design feature, making the code simpler in some ways. But the main
advantage multithreading has is to allow a program to make full use of
the available hardware.

If a process could only ever have one thread running at a time, this
advantage would not exist at all.

Note that the corralary to this is that when writing multi-threaded
code, you should design it to avoid having one thread wait on any other
thread. Some waiting is unavoidable, inasmuch as synchronizing between
threads that need to share data might require some waiting if one thread
needs access to the data while another thread currently has it. But a)
ideally the algorithm will be designed to minimize that waiting, and b)
the waiting in that scenario is implicit in the synchronization, rather
than there being a thread specifically waiting for another thread to
finish doing something (e.g. it's much worse to write code that calls
Thread.Join() than that uses the "lock" statement).

Pete
 
M

Mihajlo Cvetanović

Question 1: So will this process having four Threads be give one time slice
to each of its Threads ??

Operating system deals with threads, not with processes. Processes are
just a sort of containers for threads. So, each of those 13 threads will
be given one time slice.
Question 2: I have the understanding that if you have a computer with a
single process no more then one thread can be executing at any single time
so matter how many threads exist for this process.
Is that correct understood ?

I believe you meant "single processor", not "single process". The answer
to your question is yes, at any single time at most one thread is
running on one processor.
 
P

Peter Duniho

Mihajlo said:
[...]
Question 2: I have the understanding that if you have a computer with a
single process no more then one thread can be executing at any single time
so matter how many threads exist for this process.
Is that correct understood ?

I believe you meant "single processor", not "single process". The answer
to your question is yes, at any single time at most one thread is
running on one processor.

Since threads don't "exist for a processor", I don't see how you can
infer that he meant "processor" rather than the word "process" which he
actually wrote.

Note also that the word "processor" refers to the whole piece of
hardware, which may contain two or more _cores_. So technically, using
the current marketing terminology for CPUs, you can have multiple
threads running on a single processor, because it's a "core" that
executes a thread, not the "processor" itself.

Pete
 
F

Family Tree Mike

If every process is CPU bound — that is, all of its work require the CPU
and it doesn't ever do anything that would cause it to have to wait on
some other process or i/o — then you can make that assumption.

Of course, real life isn't anything close to being like that. But we can
start there.


In your very simplistic OS design scenario, yes. Not counting all the
other things that affect thread scheduling, threads are scheduled "round
robin", each given a fixed timeslice and will be permitted to run until
it's used that timeslice, at which time the thread will be pre-empted
and the next thread in the order will be allowed to run.


Incorrect. The thing that limits how many threads can run simultaneously
is the number of CPU cores that are present, not the number of processes
that are running.

In fact, if the OS only ever allowed one thread for each process to run
at a time, there would be very little incentive for anyone to bother
with writing a multi-threaded application. Multithreading can be a nice
design feature, making the code simpler in some ways. But the main
advantage multithreading has is to allow a program to make full use of
the available hardware.

If a process could only ever have one thread running at a time, this
advantage would not exist at all.

Note that the corralary to this is that when writing multi-threaded
code, you should design it to avoid having one thread wait on any other
thread. Some waiting is unavoidable, inasmuch as synchronizing between
threads that need to share data might require some waiting if one thread
needs access to the data while another thread currently has it. But a)
ideally the algorithm will be designed to minimize that waiting, and b)
the waiting in that scenario is implicit in the synchronization, rather
than there being a thread specifically waiting for another thread to
finish doing something (e.g. it's much worse to write code that calls
Thread.Join() than that uses the "lock" statement).

Pete

I am pretty sure question two contains a typo. The question would make
reasonable sense if it said "computer with a single processor".
 
P

Peter Duniho

Family said:
[...]
I am pretty sure question two contains a typo. The question would make
reasonable sense if it said "computer with a single processor".

See my reply to Mihajlo.

The rest of Tony's question includes similarly nonsensical scenarios.
Why should we think that he's not literally suggesting only a single
process running? Especially when he also writes "how many threads exist
for this process", and clearly understands that processes own threads.

Pete
 
T

Tony Johansson

Mihajlo Cvetanovic said:
Operating system deals with threads, not with processes. Processes are
just a sort of containers for threads. So, each of those 13 threads will
be given one time slice.


I believe you meant "single processor", not "single process". The answer
to your question is yes, at any single time at most one thread is
running on one processor.

You are understanding me correct !!

//Tony
 
P

Peter Duniho

Tony said:
[...]
I believe you meant "single processor", not "single process". The answer
to your question is yes, at any single time at most one thread is
running on one processor.

You are understanding me correct !!

Then you misunderstand how processors (or more precisely, CPU cores) and
threads work. Threads do not "exist for a processor". Any given thread
can only be executing on any one CPU core at a time, but it can be
scheduled for execution on any CPU core present (*).

(*) (For advanced multi-threading, it is actually possible to restrict a
thread to a specific CPU, but this is not normal behavior, nor is it
usually the right thing to do).

Note also that, speaking precisely, you are incorrect to say that no
more than one thread can be executing on a processor at once, since a
processor can (and for modern computers almost always does) include two
or more cores. It is the number of cores that's important, not the
number of CPUs.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top