Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Waitforsingleobject(event)

Posted on 2011-02-16
10
Medium Priority
?
588 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
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 1000 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 1000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

As with any other System Center product, the installation for the Authoring Tool can be quite a pain sometimes. This article serves to help you avoid making these mistakes and hopefully save you a ton of time on troubleshooting :)  Step 1: Make sur…
This collection of functions covers all the normal rounding methods of just about any numeric value.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

972 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