Solved

Waitforsingleobject(event)

Posted on 2011-02-16
10
548 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem with SqlConnection 4 184
Issues with C++ Class 19 108
How to Correctly derive class from CWinThread in MFC 4 89
Windows 10, Hotmail and AdChoices 7 41
MS Access 2003 or later To MySQL Migration Project Hello All, this is my second article in the category of MS-OFFICE Automation. In internet I am not able to find any comprehensive resource on the Migration of MS Access back-end to MySQL so I fin…
Many companies are making the switch from Microsoft to Google Apps (https://www.google.com/work/apps/business/). Use this article to learn more about what Google Apps has to offer and to help if you’re planning on migrating to Google Apps. It is …
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

733 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