stdthread

Is behaviour well-defined when `sleep_until()` specifies a time point in the past?

送分小仙女□ 提交于 2019-12-08 16:49:01
问题 The C++11 standard talks about what should happen if the system clock is adjusted such that the time point passed to sleep_until() is now in the past - but I can't see anywhere that addresses the case when the specified time point is already in the past. Have I simply overlooked something, or is it really not specified - even as UB or implementation-defined? A similar question arises if sleep_for() is invoked with a negative duration. 回答1: Calculation of time until which to sleep and calling

Starting a member function with arguments in a separate thread

巧了我就是萌 提交于 2019-12-08 04:45:03
问题 I have a member function MyClass::doStuff(QString & str) I am trying to call that function from a thread like this: std::thread thr(&MyClass::doStuff, this); However, that results in an error: /usr/include/c++/4.8.2/functional:1697: error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void (MyClass::*)(QString&)>(MyClass*)>’ typedef typename result_of<_Callable(_Args...)>::type result_type; So I've attempted to give it the argument: QString a("test"); std::thread thr(&MyClass:

cancelling std::thread using native_handle() + pthread_cancel()

纵然是瞬间 提交于 2019-12-07 10:06:57
问题 I am converting a previous thread wrapper around pthreads to std::thread. However c++11 does not have any way to cancel the thread. I REQUIRE, nonetheless, to cancel threads since they may be performing a very lengthy task inside an external library. I was considering using the native_handle that gives me pthread_id in my platform. I'm using gcc 4.7 in Linux (Ubuntu 12.10). The idea would be: #include <iostream> #include <thread> #include <chrono> using namespace std; int main(int argc, char

Perfect Forwarding Variadic Template to Standard Thread

て烟熏妆下的殇ゞ 提交于 2019-12-07 05:29:58
问题 I'm trying to make a form of std::thread that puts a wrapper around the code executed in the thread. Unfortunately I can't get it to compile due likely to my poor understanding of rvalues and the Function templated type I'm trying to pass. Here's my code: #include <vector> #include <thread> #include <utility> void Simple2(int a, int b) {} template <typename Function, typename... Args> void Wrapper(Function&& f, Args&&... a) { f(std::forward<Args>(a)...); } class Pool { public: template

2 threads slower than 1?

可紊 提交于 2019-12-07 00:50:02
问题 I was playing around with std::thread and something weird popped up: #include <thread> int k = 0; int main() { std::thread t1([]() { while (k < 1000000000) { k = k + 1; }}); std::thread t2([]() { while (k < 1000000000) { k = k + 1; }}); t1.join(); t2.join(); return 0; } When compiling the above code with no optimizations using clang++, I got the following benchmarks: real 0m2.377s user 0m4.688s sys 0m0.005s I then changed my code to the following: (Now using only 1 thread) #include <thread>

Telling an std::thread to kill/stop itself when a condition is met

喜欢而已 提交于 2019-12-06 20:29:20
问题 Say I have a worker thread tWorker , which is initialized when Boss is constructed and tells it to do work() , until bRetired is true. An std::mutex , mtx , locks some data ( vFiles ) so that tWorker owns it when he's working on it. How do I make tWorker "commit suicide" once bRetired becomes true ? How would the mutex be destroyed when the thread stops execution? I've read that std::thread objects cannot be interrupted in any way. Does letting the thread do nothing (or calling std::this

Error creating std::thread on Mac OS X with clang: “attempt to use a deleted function”

眉间皱痕 提交于 2019-12-06 19:52:19
问题 Consider my test code: #include <thread> class Foo { public: void threadFunc() {} void startThread() { _th = std::thread(&Foo::threadFunc, *this); } private: std::thread _th; }; int main(int argc, char *argv[]) { Foo f; f.startThread(); return 0; } This is an error it produces: ../untitled/main.cpp:13:14: warning: unused parameter 'argc' [-Wunused-parameter] int main(int argc, char *argv[]) ^ ../untitled/main.cpp:13:26: warning: unused parameter 'argv' [-Wunused-parameter] int main(int argc,

Storing an std::thread object as a class member

╄→гoц情女王★ 提交于 2019-12-06 08:20:52
I'm trying to keep an std::thread object inside a class. class GenericWindow { public: void Create() { // ... MessageLoopThread = std::thread(&GenericWindow::MessageLoop, *this); } private: std::thread MessageLoopThread; void GenericWindow::Destroy() // Called from the destructor { SendMessageW(m_hWnd, WM_DESTROY, NULL, NULL); UnregisterClassW(m_ClassName.c_str(), m_WindowClass.hInstance); MessageLoopThread.join(); } void GenericWindow::MessageLoop() { MSG Msg; while (GetMessageW(&Msg, NULL, 0, 0)) { if (!IsDialogMessageW(m_hWnd, &Msg)) { TranslateMessage(&Msg); DispatchMessageW(&Msg); } } } }

Perfect Forwarding Variadic Template to Standard Thread

强颜欢笑 提交于 2019-12-05 09:54:19
I'm trying to make a form of std::thread that puts a wrapper around the code executed in the thread. Unfortunately I can't get it to compile due likely to my poor understanding of rvalues and the Function templated type I'm trying to pass. Here's my code: #include <vector> #include <thread> #include <utility> void Simple2(int a, int b) {} template <typename Function, typename... Args> void Wrapper(Function&& f, Args&&... a) { f(std::forward<Args>(a)...); } class Pool { public: template <typename Function, typename... Args> void Binder(Function&& f, Args&&... a) { std::thread t(Wrapper<Function

2 threads slower than 1?

倾然丶 夕夏残阳落幕 提交于 2019-12-05 04:45:56
I was playing around with std::thread and something weird popped up: #include <thread> int k = 0; int main() { std::thread t1([]() { while (k < 1000000000) { k = k + 1; }}); std::thread t2([]() { while (k < 1000000000) { k = k + 1; }}); t1.join(); t2.join(); return 0; } When compiling the above code with no optimizations using clang++, I got the following benchmarks: real 0m2.377s user 0m4.688s sys 0m0.005s I then changed my code to the following: (Now using only 1 thread) #include <thread> int k = 0; int main() { std::thread t1([]() { while (k < 1000000000) { k = k + 1; }}); t1.join(); return