Multithreading - Simple std::thread::join() class

class thread_guard
{
std::thread& t;
public:
explicit thread_guard(std::thread& t_):
t(t_)
{}
~thread_guard()
{
if(t.joinable())
{
t.join();
}
}
(error1:) thread_guard(thread_guard const&)=delete;
(error2:) thread_guard& operator=(thread_guard const&)=delete;
};

Gives syntax error ';' at error1, and unexpected token(s) preceding ';' at error1.
Gives the same syntax errors for error2.

The errors aren't threading errors, however, I am fairly new to C++ objects and mutlithreading.
Last edited on
Perhaps your compiler is too old to support deleted functions. You can work around by making those two functions private and not defined.
It compiled fine.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class thread_guard
{
    std::thread &t;

public:

    explicit thread_guard(std::thread &t_)
    :
    t(t_)
    {}

    ~thread_guard()
    {
        if(t.joinable())
        {
            t.join();
        }
    }
};

Cubbi, thanks for the answer. I will try that!

Bourgond Aries, the (error1-2:) sentences are included in the class
Topic archived. No new replies allowed.