Meaning of Thread and sleep_for()

Goodmorning everyone.
I'm a beginner in using C++. I have enclosed a code found on web that computes the duration of a specific calculation (in this case the time spent to write on the console the number 5).
My question refers to "thread". what is its meaning in this code? My attention, in particular, is focused on "sleep_for(1s)". Is this affecting the calculation time? If yes, how?

Anyway, my task is computing the time from the start of a first calculation (that is defined in a first function) to the end of a second calculation (that is defined in a second function). In other words the time I'm looking for is the sum of the time of each calculation where every calculation is specified in a specific function. How can I do it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <chrono>
#include <thread>

using namespace std;

int main()
{
	auto start = chrono::high_resolution_clock::now();
	//this_thread::sleep_for(1s);

	int number = 5;
	cout << number << endl;

	auto end = std::chrono::high_resolution_clock::now();
	chrono::duration<float> duration = end - start;
		
	cout << duration.count() << "s";
		
	cin.get();

}


Thank you for your attention and availability
Last edited on
this_thread::sleep_for(1s) means the current thread should wait at least one second before continue to execute the rest of the code.
Note that the s is a user defined literal (from the standard library) that denotes a second. See:

https://en.cppreference.com/w/cpp/language/user_literal
> Anyway, my task is computing the time of a calculation:
> I want the time from the start of a calculation to the end of this one. How can I do it?

Something along these lines:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <chrono>
#include <type_traits>
#include <ctime>
#include <utility>
#include <thread>

struct timer {

    // to measure real time, use the high resolution clock if it is steady, the steady clock overwise
    using clock_type = std::conditional< std::chrono::high_resolution_clock::is_steady,
                                         std::chrono::high_resolution_clock,
                                         std::chrono::steady_clock >::type ;
    
    // return a pair of values: elapsed proceesor time, elapsed real time since start
    std::pair<double,long> elapsed_millisecs() const {

         const auto processor_now = std::clock() ;
         const auto real_now = clock_type::now() ;

         using namespace std::chrono ;
         return { (processor_now-processor_start) * 1000.0 / CLOCKS_PER_SEC,
                  duration_cast<milliseconds>(real_now-real_start).count() } ;
     }

    clock_type::time_point real_start = clock_type::now() ; 
    std::clock_t processor_start = std::clock() ;
};

int main() {

    timer t ; // instantiate the timer at the start of the calculation

    // perform the long calculation
    volatile double d = 1.0 ;
    for( int i = 0 ; i < 1'000 ; ++i )
    {
        for( int j = 0 ; j < 10'000 ; ++j ) d += 1.0001 ;
        
        // introduce an artificial delay to illustrate the difference between processor time and real time 
        std::this_thread::sleep_for( std::chrono::milliseconds(1) ) ; 
    }
    
    // get the elapsed times immediately after the calculation is over
    const auto [p,r] = t.elapsed_millisecs() ; // p is the elapsed processor time, r is the elapsed real time
    
    std::cout << p << " milliseconds processor\n" << r << " milliseconds real\n" ;
}

http://coliru.stacked-crooked.com/a/ee64691e1c9e6a3f
from your question, I am reading that you do not know what the words mean. I would suggest looking it up, but the terminology begats more terminology and you have to start somewhere...

a thread is a process that is executing on your machine. Specifically, it is a child process of a program's main process. Threads are how one gets the most out of modern multi-CPU computers, as each thread can execute on one of your cores (core= one cpu, most computers have 2-4 of these, some have more, very few have only 1 these days).

Sometimes a thread needs to wait on something else. For example if you have a thread to compute some math stuff, but it needs the result of another thread that has not finished yet, it has to wait. Sleep tells a thread to wait for a while and check again to see if it can go forward. Sleep can also be used as a crude timer delay, though the delay of a sleep is not generally exact. Its good enough when talking seconds, but you wouldn't want to try to make a long-running clock program with it.



> a thread is a process that is executing on your machine.
> Specifically, it is a child process of a program's main process.

No.

This a succinct description of what a thread is:
A thread of execution is a sequence of instructions that can be executed concurrently with other such sequences in multithreading environments, while sharing a same address space.
http://www.cplusplus.com/reference/thread/thread/
Topic archived. No new replies allowed.