Talk:cpp/thread/thread/thread
From cppreference.com
Is this even possible to call std::thread with "a pointer to a member data object of a class"? What does it do?
I think the invoke template should not be used for std::thread because passing a data member makes no sense for a thread.
- It is possible:
Run this code
#include <iostream>
#include <thread>
struct S {
int data;
};
int main() {
S s;
std::thread t(&S::data, s);
std::cout << "Launched the thread, tid = " << t.native_handle() << '\n';
t.join();
}
- Works in clang++/libc++ too. We could make a Note mentioning that it's pointless to do so - it's not the only thing C++ allows that's pointless. --Cubbi 18:46, 28 August 2013 (PDT)
- You can use side effects of operations, strange way, but works:
Run this code
#include <thread>
#include <iostream>
#include <mutex>
std::mutex cout_lock;
struct Value {
~Value() {
std::lock_guard<std::mutex> lock(cout_lock);
std::cout << "Value dtor (id: " << std::this_thread::get_id() << ")\n";
}
};
struct A {
Value value;
};
class B {
public:
A& operator*() {
std::lock_guard<std::mutex> lock(cout_lock);
std::cout << "B::operator* (id: " << std::this_thread::get_id() << ")\n";
return a_;
}
public:
static A a_;
};
int main() {
std::cout << "main thread = " << std::this_thread::get_id() << std::endl;
A a;
B b;
std::thread thread_1(&A::value, a);
// (return t1.*f) dtor invoked in other thread
std::thread thread_2(&A::value, b);
// ((*t1).*f) B::operator* invoked in other thread
thread_1.join();
thread_2.join();
}
Ruslo 01:50, 29 August 2013 (PDT)
Suggestion to maintainers.
Where new constructs or syntax are used please provide a reference to explain it.
i.e.
template< class Function, class... Args > explicit thread( Function&& f, Args&&... args );
I don't understand the usage of && of the first parameter.
--JRHeisey (talk) 16:20, 1 December 2017 (PST)