- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi everybody,
Hope that this is right forum for my question.
i am experimenting with native Win32 multithreading and trying to force currently executing to thread to relinquish its cpu time by calling Win API function sleep() with 0 argument.When sleep(0) is called from within thread function then currently executing thread will be swapped and put in wait queue.
One interesting question arises which is related to exact moment of quantum interval when the execution is postponed and how to control it programmatically.If new thread is created from within the main function thread and that new thread has priority rised to high and it is scheduled to run immediatly after creation so how (inside thread's function) or better when sleep(0) call will be executed in order for example to stop the execution after 1/2 of quantum expires.I am thinking about the inserting call to sleep at begining oof the function thread and verifying it in disassembly so I could be able to theoretically postpone the execution as a function of location(at address space) of call instruction.By insering some job/task or calculation which will keep thread busy for quantum period ~30ms and insering inside loop call to sleep function I can postpone the execution of thread before it quantum expires.
Does anyone have some experience with this?
Source code is included.
Thanks in advance
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Sorry it should be "quantum expiration"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
sleep(0) relinquishes remainder of time-slice and places current thread at end of current thread's priority of run queue (which may have 0 or more pending threads).
In your code supplied, and relating to your concern about initiating thread being of lower priority of launched threads, have the initiating thread create a single event with manual reset and initialized to the reset state. Then create the additional higher priority threads. The start of the thread function should have a WaitForSingleEvent on the new event object (IOW all newly created highr priority threads will wait until the initiating thread signals completion of launching all new threads).
Jim Dempsey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
By the way, should you use sched_yield() or equivilent Windows function SwitchToThread, then note that the current thread relinquishes remaining thread time slice and is not inserted at the back of the current priority queue, but rather is inserted in between the curent runable threads at its priority level and the threads waiting for I/O completion (or other event) having the same priority level. These waiting for I/O or event threads will not progress out of this this state on I/O completion (or other event) in front of a sched_yield()-ing thread. (at least this was my experience). The net effect is if the high priority thread is in a polling loop with sched_yield() waiting for a different thread to complete an I/O and there are insufficient cores available for it to run, then the high priority polling threads will not permit the I/O completion thread to run to the point of setting its "done" flag. (IOW use sleep(0))
Jim Dempsey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks Jim for answer.
I must admit that uploaded code still does not implement calls to thread priority functions.My question was more related to how in controllable way postpone the execution of thread before quantum expiration.I mean if is it possible to do it only by changing the location at (assembly level) of call to sleep(0) function.Thread must execute at high priority in order to be not postponed by other more priviledged thread before the call to sleep().
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I did not see your second answer when I posted my response.I will do as you advised me and report back the results.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
>>Thread must execute at high priority in order to be not postponed by other more privileged thread before the call to sleep().
What differenc does it make if the thread about to execute sleep(0) gets preempted or time sliced out?
In my earlier post the term "Should" is a conditional clause like an "if" statement in programming.
If (should) you desire to use compute loops using sleep(0) when nothing to do then either use events (Windows) or condition variables (pthread) or other constructs available to you for example a mutex or semaphore etc.... An alternative technique is after n trips through sleep(0) with nothing to do, then tweak up the sleep duration: sleep(1), 10, 100,....
In your posted code, The thread running ThreadFunc will compute its result, search the ThreadArr for its thread handle, print info, sleep(0), then resume searching ThreadArr, then exit. There is no logic to what you are attempting to do. This thread will almost immeiately exit after searching the remainder of ThreadArr.
Jim Dempsey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
>>>There is no logic to what you are attempting to do. This thread will almost immeiately exit after searching the remainder of ThreadArr.>>>
I know that.Uploaded code is still unfinished and call to sleep from within ThreadArr loop was used for testing with debugger.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
This exactly what I plan to do.Executing thread will have its priority set to High or even Real Time and RDTSC instruction will be used to measure for-loop block to gauge the exact time needed to accomplish the task and later call to sleep(0) eill be issued.The other option is to use some logic inside for loop to postpone the execution after some number of loop iterations or even try to postpone the execution after some time was spent executing the code.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks for info.
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page