stdthread

std::thread pass by reference calls copy constructor

自作多情 提交于 2019-11-26 05:57:15
问题 Well I have an issue with passing data into a thread using std::thread. I thought I understood the general semantics of copy constructors, etc. but it seems I don\'t quite grasp the problem. I have a simple class called Log that has hidden it\'s copy constructor thusly: class Log { public: Log(const char filename[], const bool outputToConsole = false); virtual ~Log(void); //modify behavior void appendStream(std::ostream *); //commit a new message void commitStatus(const std::string str);

Passing object by reference to std::thread in C++11

百般思念 提交于 2019-11-26 03:39:19
问题 Why can\'t you pass an object by reference when creating a std::thread ? For example the following snippit gives a compile error: #include <iostream> #include <thread> using namespace std; static void SimpleThread(int& a) // compile error //static void SimpleThread(int a) // OK { cout << __PRETTY_FUNCTION__ << \":\" << a << endl; } int main() { int a = 6; auto thread1 = std::thread(SimpleThread, a); thread1.join(); return 0; } Error: In file included from /usr/include/c++/4.8/thread:39:0,

Thread pooling in C++11

坚强是说给别人听的谎言 提交于 2019-11-26 02:39:52
问题 Relevant questions : About C++11: C++11: std::thread pooled? Will async(launch::async) in C++11 make thread pools obsolete for avoiding expensive thread creation? About Boost: C++ boost thread reusing threads boost::thread and creating a pool of them! How do I get a pool of threads to send tasks to , without creating and deleting them over and over again? This means persistent threads to resynchronize without joining. I have code that looks like this: namespace { std::vector<std::thread>

What happens to a detached thread when main() exits?

若如初见. 提交于 2019-11-26 00:51:04
问题 Assume I\'m starting a std::thread and then detach() it, so the thread continues executing even though the std::thread that once represented it, goes out of scope. Assume further that the program does not have a reliable protocol for joining the detached thread 1 , so the detached thread still runs when main() exits. I cannot find anything in the standard (more precisely, in the N3797 C++14 draft), which describes what should happen, neither 1.10 nor 30.3 contain pertinent wording. 1 Another,

What happens to a detached thread when main() exits?

徘徊边缘 提交于 2019-11-26 00:09:08
Assume I'm starting a std::thread and then detach() it, so the thread continues executing even though the std::thread that once represented it, goes out of scope. Assume further that the program does not have a reliable protocol for joining the detached thread 1 , so the detached thread still runs when main() exits. I cannot find anything in the standard (more precisely, in the N3797 C++14 draft), which describes what should happen, neither 1.10 nor 30.3 contain pertinent wording. 1 Another, probably equivalent, question is: "can a detached thread ever be joined again", because whatever