Solved

Waitforsingleobject(event)

Posted on 2011-02-16
10
540 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

The System Center Operations Manager 2012, known as SCOM, is a part of the Microsoft system center product that provides the user with infrastructure monitoring and application performance monitoring. SCOM monitors:   Windows or UNIX/LinuxNetwo…
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
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 …

813 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

11 Experts available now in Live!

Get 1:1 Help Now