Solved

Waitforsingleobject(event)

Posted on 2011-02-16
10
566 Views
Last Modified: 2012-05-11
Hello
windows os scheduler schedules a process, it does not know whether process is single/multiple threaded, c++ multithreaded binary has thread scheduler which internally schedules each thread of that process, when os scheduler gives timeslice to this process, if my perception is correct,
Please let me know, if thread1 in the process executes waitforsingleobject(m-event) , does that thread goes in a state similar to a wait state when thread scheduler keeps a thread in a wait state?
Please let me know the answer
Sham
 
0
Comment
Question by:mohet01
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 

Expert Comment

by:araud
ID: 34913645
Hi, Sham!

Your understanding is quite correct.
Any WinAPI waiting operation suspends the thread and excludes it from OS scheduler until event you are waiting for.
It doesn't matter if that thread belongs to "single-threaded" or "multi-threaded" process.

In fact there is no special conception of multi-threaded process, process just can have more than one threads but not less than one.
But some libraries have different implementations or ways to initialize them up depending on how many threads you going to use them from. And this is linked to the fact that working in one thread is not as dangerous as accessing the same data from several threads.

So just keep in mind that creation of any additional thread beside the main one (created by process loader) takes careful synchronization between them: shared data and threads life time.

Alexander.
0
 

Author Comment

by:mohet01
ID: 34913654
Hello
I really did not get this update
Sham
0
 

Expert Comment

by:araud
ID: 34913757
Sorry, may be I didn't get your question right then.

"if thread1 in the process executes waitforsingleobject(m-event) , does that thread goes in a state similar to a wait state when thread scheduler keeps a thread in a wait state?"

If any thread in any process calls any of "Wait..." functions it is getting excluded from scheduler and doesn't receive time slice.
When event happens, thread is included in scheduler again and start receiving time slices.

Please rephrase your question if it still doesn't help you.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 12

Expert Comment

by:trinitrotoluene
ID: 34914007
there is a difference.

When a thread waits for an object to be signaled it cannot be scheduled by the processor since it is not in the "ready" state.

On the other hand a thread which is in the Ready queue and is waiting for processor time could be scheduled when its turn comes in.

So you see both are not the same.....

http://msdn.microsoft.com/en-us/library/ms682105%28VS.85%29.aspx
http://msdn.microsoft.com/en-us/library/ms687069%28v=vs.85%29.aspx
0
 

Expert Comment

by:araud
ID: 34914108
Thank you, trinitrotoluene!

Those are details of implementation of scheduler algorithm. And each new Windows version usually have some changes in implementation.

But your answer is more exact, thank you!
0
 

Author Comment

by:mohet01
ID: 34914820
Hello
Based on your updates, I need to seriously clarify , what do u feel about scheduling of a process which has multiple threads, before discussing waitforsingleobject(event) query.
Confirm if the below is correct:

"I wrote a Windows c/c++ app whose binary is scheduled as a process(say process id 1234), Windows OS scheduler schedules this process 1234, OS scheduler does not know whether process 1234 is single/multiple threaded. When OS scheduler gives say 5 msec to this process 1234, _init()function[linker adds this code during link time]  of process 1234 has thread scheduler logic which internally schedules each thread of that process within 5 msec."

IS MY UNDERSTANDING CORRECT?!!!!!!!!!!!!!!!!!!!!!!
Sham

0
 

Assisted Solution

by:araud
araud earned 250 total points
ID: 34914886
OS scheduler doesn't care much which process thread belongs to. Well it does to some extend, but to simplify things let's count it doesn't in context of your question. (Process priority influences to priorities of its threads. But this is different topic.)

And scheduler doesn't give time slices to process, but it gives time slices to threads.
Process is address space, thread is execution unit.

Process has no separate scheduler inside to manage it threads.

So all threads it created go to single OS scheduler with all other threads of other processes.
There is only one scheduler for all threads of all processes.

However if you want to manage your threads manually, there are "fibers".
0
 

Author Comment

by:mohet01
ID: 34914932
Hello
If you say that:
"So all threads it created go to single OS scheduler with all other threads of other processes.
There is only one scheduler for all threads of all processes"

1) So, If a createthread() api gets invoked with a function name as an argument in my process one new thread gets added to run queue of Windows OS scheduler?
2) Is this visible in Task manager of my windows machine?

Sham
0
 

Accepted Solution

by:
araud earned 250 total points
ID: 34915123
1) yes
2) yes. You can see increase of thread count in your process after that (Process tab) and common thread count (Performance tab: System->Threads).
0
 

Author Closing Comment

by:mohet01
ID: 34915610
Done
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The new Microsoft OS looks great, is easier than ever to upgrade to, it is even free.  So what's the catch?  If you don't change the privacy settings, Microsoft will, in accordance with the (EULA) you clicked okay to without reading, collect all the…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Viewers will learn the different options available in the Backstage view in Excel 2013.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question