3

How does the clock function work behind the scene in a multithreading program?

AFAIK, the clock function provided by the C standard library could be used to measure the CPU time consumed by the running program, but how does this process work under the hood? Is the underlying hardware timer part of the CPU chip? If it's not, how does the clock know when the CPU is scheduled to run the current program? Since the clock function only records the time consumption of each individual CPU that executes the current program. Meaning in a multi-threaded program, the returned value would be lower than the wall-clock time.

*Although I raised another question in What's the relationship between the real CPU frequency and the clock_t in C?, but actually, those are two different topics, so, I don't want to mix them up in one post.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Jason Yu
  • 1,886
  • 16
  • 26
  • 1
    @user2864740 Nope, this one talks about clock function and multithreading programs. Not regarding the real CPU frequency mentioned in that post. – Jason Yu Jan 09 '22 at 09:41
  • Note that on Windows the `clock` function doesn't measure the elapsed CPU time of a process, but it follows the wall clock. – Some programmer dude Jan 09 '22 at 09:58
  • Aside: in MS VC the `clock()` function is non-conforming (no surprise there) and it measures wall time. – Weather Vane Jan 09 '22 at 09:58
  • Isn't this like "how long is a piece of string?" C18 §7.27.2.1 says *"The clock function returns the implementation’s **best approximation** ... If the processor time used is not available, the function returns the value (clock_t)(−1)."* If the CPU has a hardware timer, the OS might use that. If it has a RTC, it might use that. And so on. – Weather Vane Jan 09 '22 at 10:05
  • @JasonYu Your latest edit has made my answer look a bit daft. Maybe you should roll it back? (Especially as it doesn't really make sense when you *still* have the assumption that it is a per-CPU timer.) – Adrian Mole Jan 09 '22 at 10:11
  • @AdrianMole I think it should be fine since what I really want to know is what exactly happened from the underlying (os, CPU, implementation of the `clock` fucntion) components perspertive. And I don't want to confuse people with the wrong execution effect, so a correction is needed here. – Jason Yu Jan 09 '22 at 10:21
  • 1
    @jason But the correction is part of my answer. You should *not* edit information from answers into your question. – Adrian Mole Jan 09 '22 at 10:30

1 Answers1

2

Since the clock function only records the time consumption of each individual CPU that executes the current program. Meaning in a multi-threaded program, the returned value would be lower than the wall-clock time.

I can't find a definitive statement in a C Standard but, according to cppreference (which is generally very reliable), your assumption is wrong and the clock() function returns the total (for all CPUs) processor time used by the program (bold emphasis mine):

… For example, if the CPU is shared by other processes, clock time may advance slower than wall clock. On the other hand, if the current process is multithreaded and more than one execution core is available, clock time may advance faster than wall clock.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83