How passing argument as const reference works?

30 Views Asked by At

I am not able to figure out why output of the code is 10. I have created one function printA that excepts int as const reference, from main I have created one thread and called printA, thread is kept to sleep for 20 secs so that variable a is modified by main thread.

But still output is

value of a changed
10
#include <iostream>
#include <thread>
#include<unistd.h>     
using namespace std;

void printA(const int& a)
{
    sleep(20);
    cout<<a;
}

int main()
{
    int a = 10;
    std::thread t(printA, a);
    a = 20;
    cout<<"value of a changed"<<endl;
    t.join();
}

Any help is very much appreciated.

0

There are 0 best solutions below