From: Tony Johansson on
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


From: Peter Duniho on
Tony Johansson wrote:
> 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
From: Mihajlo Cvetanović on
On 5/8/2010 3:02 PM, Tony Johansson wrote:
> 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.
From: Peter Duniho on
Mihajlo Cvetanović wrote:
> [...]
>> 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
From: Family Tree Mike on
On 5/8/2010 12:07 PM, Peter Duniho wrote:
> Tony Johansson wrote:
>> 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

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

--
Mike