Link to home
Start Free TrialLog in
Avatar of F-J-K
F-J-KFlag for Canada

asked on

Questions About Multi-Threading in C++ Under Windows! I Need Clear Simple Answer Please

Quote: "The system treats all threads with the same priority as equal. The system assigns time slices in a round-robin fashion to all threads with the highest priority. If none of these threads are ready to run, the system assigns time slices in a round-robin fashion to all threads with the next highest priority. If a higher-priority thread becomes available to run, the system rejects to execute the lower-priority thread (without allowing it to finish using its time slice), and assigns a full time slice to the higher-priority thread. Round-robin (RR) is one of the simplest scheduling algorithms for processes in an operating system, which assigns time slices to each process in equal portions and in order, handling all processes without priority."

>>If a higher-priority thread becomes available to run, the system rejects to execute the lower-priority thread (without allowing it to finish using its time slice), and assigns a full time slice to the higher-priority thread

Q1.  the process will only run one thread at a time since the system takes the highest priority thread only, so all other threads will be rejected! We conclude with one thread running, similar to single-threading. Can you clarify please?

>1>The system assigns time slices in a round-robin fashion to all threads with the highest priority.
>2>Round-robin (RR) is one of the simplest scheduling algorithms for processes in an operating system, which assigns time slices to each process in equal portions and in order, handling all processes without priority

Q2.In quote 2, round-robin algorithm handles all process without priority. In quote 1, it says the system assigns time slices in a round-robin fashion to all threads with the highest priority.

RR algorithm handles all process without priority, but in case of threading RR cares about priority. Right?

I hope to hear from you. I could not find clear answers! I hope you can finish that for me
Avatar of sunnycoder
sunnycoder
Flag of India image

>Q1.  the process will only run one thread at a time since the system takes the highest priority thread only, so all
>other threads will be rejected! We conclude with one thread running, similar to single-threading.
At any time, on one processor, only one thread/process can execute. Multithreading is most helpful for multiprocessor systems.
Your understanding is correct ... if there is only one procesor, high pri thread will run .. others would wait in a queue

>Q2.In quote 2, round-robin algorithm handles all process without priority. In quote 1, it says the system assigns
>time slices in a round-robin fashion to all threads with the highest priority.
>RR algorithm handles all process without priority, but in case of threading RR cares about priority. Right?
Typically if there are n priority levels, you would have n queues. OS would look for threads in highest priority queue. All of the threads in the queue would get processor in RR fashion. Once all of these threads have finished, OS would look for process in next level. You can visualize it as priority queues with RR in each queue.
Avatar of F-J-K

ASKER

>> if there is only one proccesor, high pri thread will run .. others would wait in a queue

Yes, but one processor can have multi-threads & this quote is taken from multi-threading msdn guide. Here we are talking about a program that runs many activities at once. Its not fair, if i run 3 activities that keeps running, in a sudden a higher priority activity appears. All the 3 activities will be killed automatically, just to server the higher priority one. If thats the care, why do we even care to make a multi-threading source?

>>Typically if there are n priority levels, you would have n queues. OS would look for threads in highest priority queue.

levels range 0 to 31, so level 31 is the highest priority. Every level has a queue. Thus, a multi-threading application makes a queue for every level. Level 31 can have a queue of 10 threads, level 30 can have 4 threads, and so on. Level 31 has 10 threads - these 10 threads starts from 0 to 9, and system goes from thread 9 to 0. Once finished, system takes the next level queue, which is 30 & so on. I hope this right. Correct?

Please note, i'm talking about multi-threading not multi-processing i just do not want to mix them up.

I appreciate your help sunnycoder!
Avatar of F-J-K

ASKER

If my second answer is correct, then the first one is solved, since when  high priority thread  is mentioned, that actually means the whole level that got queue of high priority threads.
ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of F-J-K

ASKER

GREAT - Very Clear! Few more questions. Please bear with me.

"Each process is started with a single thread, often called the primary thread, but can create additional threads from any of its threads."

The primary thread is "int main()". Right? As without int main(), other threads wouldn't exist.

"Threads are scheduled to run based on their scheduling priority. Each thread is assigned a scheduling priority. The priority levels range from zero (lowest priority) to 31 (highest priority). Only the zero-page thread can have a priority of zero. (The zero-page thread is a system thread responsible for zeroing any free pages when there are no other threads that need to run.)"

Is this zero-page thread is created immediately after the a program is executed (process occurred)?
Is every running process in a machine has its own a zero-page thread?

The priority level range from 0 to 31. If 0 is a system thread, then why don't we say a process (program) have priority from 1 to 30! since 0 is a system thread. system thread means its created when OS boots...I do not know, any suggestions?


Many Thanks for you support


Avatar of F-J-K

ASKER

Is base priority has number 0? Or base priority has to be specified by the developer?
>The primary thread is "int main()". Right?
correct

>Is this zero-page thread is created immediately after the a program is executed (process occurred)?
>Is every running process in a machine has its own a zero-page thread?
The zero-page thread is a **system** thread  - not per process.

>why don't we say a process (program) have priority from 1 to 30!
Wouldn't you like to know about level 0? Even if you cant use it, it makes the picture complete.

>Is base priority has number 0? Or base priority has to be specified by the developer?
Base priority would not be 0 .. It would be some number ... there would be some default value for it and developers can specify priority using provided APIs
Avatar of F-J-K

ASKER

Ha Ok, i see! Here is the last questions and i'm done......

"The scheduler maintains a queue of executable threads for each priority level. These are known as ready threads. When a processor becomes available, the system performs a context switch."

>>these are known as ready threads

when i usually run multi-threaded application, every time i want to do a new activity i just click what i want to do! Happens fast, i never waited for some threads to finish, so other can be launched. Like all of them have high priority, all of them are always ready. Anyway, not a big deal...

*Every level can carry how many threads? 0 to 31 levels ...

*When is base priority occurs? When does it take place, why base priority is needed?


*Every level can carry how many threads? 0 to 31 levels ...
Implementation specific.

*When is base priority occurs? When does it take place, why base priority is needed?
Base priority is defined at the time of creation of thread. It is needed to determine what queue this thread belongs to.
Avatar of F-J-K

ASKER

All I Can Say: You Know How Newbies Think! THANKS for Support