1

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?

GhostCat
  • 137,827
  • 25
  • 176
  • 248
Water Cooler v2
  • 32,724
  • 54
  • 166
  • 336

1 Answers1

2

Interesting question; but I think: probably not.

In order to measure this from inside the thread, the thread needs to know about two events:

  1. Thread is started/resumed; gets CPU time; and starts running
  2. Thread is "suspended", and stops execution

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".

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Thank you. I was doubtful too because of the reasons you mention. If that's not possible, would it be possible to just read a variable from an operating system structure that tells you how much a time-slice measures? – Water Cooler v2 Apr 19 '17 at 07:52
  • I updated my answer; but still on the meta-level; as I am missing the in-depth .Net and Windows OS knowledge to give a really technical answer. – GhostCat Apr 19 '17 at 08:01