Solved

Waitforsingleobject(event)

Posted on 2011-02-16
10
531 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
 
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This very simple solution applies to a narrow cross-section of the "needs to close" variety. In this case, the full message in Event Viewer was in applog, Event ID 1000: Faulting application iexplore.exe, version 8.0.6001.18702, faulting module …
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now