I'm reading the book C++ Concurrency in Action to learn more about threading and the C++ memory module. I'm curious about the number of times the copy constructor is called in the following code:
struct func
{
func() = default;
func(const func& _f) {}
void operator()() {}
};
int main()
{
func f;
std::thread t{ f };
t.join();
return 0;
}
When I walk through this code in the Visual Studio 2013 debugger, I see the copy-constructor called four separate times. It's called three times from the main thread and then once from the new one. I was expecting one, as it made a copy of the object for the new thread. Why are three extra copies created?