stdthread

I want to kill a std::thread using its thread object? [duplicate]

a 夏天 提交于 2019-11-28 04:32:33
Possible Duplicate: C++0x thread interruption I am trying to kill/stop a c++ std::thread by using its thread object. How can we do this? Matthieu M. @bamboon's answer is good, however I feel this deserves a stronger statement. Whatever the language you use, your program will acquire and release resources: memory, file descriptors, ... For simple programs that are fired in one shots, leaking resources does not matter much: when the program ends modern OSes automatically take the resources back; however for long-running programs a basic requirement is not to leak resources, or at least not

Massive CPU load using std::lock (c++11)

狂风中的少年 提交于 2019-11-28 03:34:57
My recent efforts to implement a thread/ mutex manager ended up in an 75% CPU load (4 core), while all four running threads were either in sleep or waiting for a mutex beeing unlocked. The specific class is far too large for being posted here entirely, but I could narrow down the cause to the deadlock-safe acquiring of two mutexes std::unique_lock<std::mutex> lock1( mutex1, std::defer_lock ); std::unique_lock<std::mutex> lock2( mutex2, std::defer_lock ); std::lock( lock1, lock2 ); Another part of the class uses a std::condition_variable with wait() and notify_one() on mutex1 for some code to

C++11 'native_handle' is not a member of 'std::this_thread'

拜拜、爱过 提交于 2019-11-28 00:56:17
In the following code snippet, void foo() { std::this_thread::native_handle().... //error here } int main() { std::thread t1(foo); t1.join(); return 0; } How do you get the native_handle from std::this_thread from within the function foo ? There is no way for a thread to autonomously gain access to its own std::thread . This is on purpose since std::thread is a move-only type. I believe what you're requesting is a native_handle() member of std::thread::id , and that is an interesting suggestion. As far as I know it is not currently possible. It would be used like: void foo() { auto native_me =

C++11: What happens if you don't call join() for std::thread

此生再无相见时 提交于 2019-11-27 20:26:42
Given below: void test() { std::chrono::seconds dura( 20 ); std::this_thread::sleep_for( dura ); } int main() { std::thread th1(test); std::chrono::seconds dura( 5 ); std::this_thread::sleep_for( dura ); return 0; } main will exit after 5 seconds, what will happen to th1 that's still executing? Does it continue executing until completion even if the th1 thread object you defined in main goes out of scope and gets destroyed? Does th1 simply sits there after it's finished executing or somehow gets cleaned up when the program terminates? What if the thread was created in a function, not main -

std::thread::join() hangs if called after main() exits when using VS2012 RC

╄→尐↘猪︶ㄣ 提交于 2019-11-27 13:56:51
The following example runs successfully (i.e. doesn't hang) if compiled using Clang 3.2 or GCC 4.7 on Ubuntu 12.04, but hangs if I compile using VS11 Beta or VS2012 RC. #include <iostream> #include <string> #include <thread> #include "boost/thread/thread.hpp" void SleepFor(int ms) { std::this_thread::sleep_for(std::chrono::milliseconds(ms)); } template<typename T> class ThreadTest { public: ThreadTest() : thread_([] { SleepFor(10); }) {} ~ThreadTest() { std::cout << "About to join\t" << id() << '\n'; thread_.join(); std::cout << "Joined\t\t" << id() << '\n'; } private: std::string id() const {

Portable way of setting std::thread priority in C++11

拈花ヽ惹草 提交于 2019-11-27 11:39:46
What is the correct way in the post C++11 world for setting the priority of an instance of std::thread Is there a portable way of doing this that works at least in Windows and POSIX (Linux) environments? Or is it a matter of getting a handle and using whatever native calls are available for the particular OS? Mike Seymour There's no way to set thread priorities via the C++11 library. I don't think this is going to change in C++14, and my crystal ball is too hazy to comment on versions after that. In POSIX, pthread_setschedparam(thread.native_handle(), policy, {priority}); I don't know the

Confusion about threads launched by std::async with std::launch::async parameter

爱⌒轻易说出口 提交于 2019-11-27 11:09:30
问题 I am a little bit confused about the std::async function. The specification says: asynchronous operation being executed "as if in a new thread of execution" (C++11 §30.6.8/11). Now, what does that supposed to mean? In my understanding, the code std::future<double> fut = std::async(std::launch::async, pow2, num); should launch the function pow2 on a new thread and pass the variable num to the thread by value, then sometime in the future, when the function is done, place the result in fut (as

MinGW and std::thread

…衆ロ難τιáo~ 提交于 2019-11-27 06:49:56
问题 So I've been trying to get the following code to compile and run on Windows by using a MinGW compiler. #include <iostream> #include <thread> void test() { std::cout << "test" << std::endl; } int main() { std::thread t(test); } I'm compiling with the following command: g++ -std=c++11 test.cpp -o test.exe Now the problem is the version of MinGW one should use and I've tried about all the versions I know of. MinGW-builds: thread-win32 MinGW-builds: thread-posix MinGW-w64: stdthread experimental

Massive CPU load using std::lock (c++11)

﹥>﹥吖頭↗ 提交于 2019-11-27 05:10:45
问题 My recent efforts to implement a thread/ mutex manager ended up in an 75% CPU load (4 core), while all four running threads were either in sleep or waiting for a mutex beeing unlocked. The specific class is far too large for being posted here entirely, but I could narrow down the cause to the deadlock-safe acquiring of two mutexes std::unique_lock<std::mutex> lock1( mutex1, std::defer_lock ); std::unique_lock<std::mutex> lock2( mutex2, std::defer_lock ); std::lock( lock1, lock2 ); Another

C++11 'native_handle' is not a member of 'std::this_thread'

你。 提交于 2019-11-27 04:07:19
问题 In the following code snippet, void foo() { std::this_thread::native_handle().... //error here } int main() { std::thread t1(foo); t1.join(); return 0; } How do you get the native_handle from std::this_thread from within the function foo ? 回答1: There is no way for a thread to autonomously gain access to its own std::thread . This is on purpose since std::thread is a move-only type. I believe what you're requesting is a native_handle() member of std::thread::id , and that is an interesting