Home > Backend Development > C++ > Why Does std::thread Call the Copy Constructor Even When Passing by Reference?

Why Does std::thread Call the Copy Constructor Even When Passing by Reference?

Linda Hamilton
Release: 2024-11-16 15:22:03
Original
194 people have browsed it

Why Does std::thread Call the Copy Constructor Even When Passing by Reference?

std::thread Calls Copy Constructor When Passing by Reference

Passing data into a thread using std::thread can be problematic, especially when dealing with classes that have disabled copy constructors. This behavior stems from the fact that std::thread takes its arguments by value.

Consider the following class Log with a disabled copy constructor:

class Log
{
public:
    Log(const char filename[], const bool outputToConsole = false);
    virtual ~Log(void);

    // ...

private:
    // Disable copy constructor and assignment operator
    Log(const Log &);
    Log &operator=(const Log &);
};
Copy after login

In the main function, the Log object logger is passed by reference to the server function:

Log logger("ServerLog.txt", true);
server(ioService, portNumber, logger);
Copy after login

The server function then creates a new thread with logger as an argument:

std::thread newThread(session, &sock, logger);
Copy after login

However, this causes a compiler error due to the disabled copy constructor in Log.

Why Does Passing by Reference Call the Copy Constructor?

Contrary to common assumptions, passing by reference in C does not prevent the invocation of the copy constructor. When passing a reference, the compiler creates a temporary copy of the referred object and passes it to the function. This is because std::thread expects its arguments to be copyable.

Solution: Using std::reference_wrapper

To achieve reference semantics in this scenario, you can use std::reference_wrapper:

std::thread newThread(session, &sock, std::ref(logger));
Copy after login

std::ref wraps a reference in a way that makes it appear as a copyable object to std::thread. However, you must ensure that the lifetime of the underlying object (in this case, logger) extends beyond the lifetime of the thread.

The above is the detailed content of Why Does std::thread Call the Copy Constructor Even When Passing by Reference?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template