I understand the actual time slice is variable depending on several factors such as the OS and the processor.
But is there a way I can make a thread count the time slice allotted to it during the time it has the CPU to itself?
I understand the actual time slice is variable depending on several factors such as the OS and the processor.
But is there a way I can make a thread count the time slice allotted to it during the time it has the CPU to itself?
Interesting question; but I think: probably not.
In order to measure this from inside the thread, the thread needs to know about two events:
From there: even if you find a mechanism to tell a thread "you have just been resumed" - how could you tell the thread when it was suspended? In other words: you would need two hooks, that somehow notify the thread not only about "you are resumed"; but also about "you will be suspended soon"; so that the thread itself can do its bookkeeping.
Given your comment: yes, if the underlying OS allows to determine the duration of a CPU slice and the duration time is constant; then the thread would not need to know about "event 2".