stdthread

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

巧了我就是萌 提交于 2019-11-27 00:28:27
问题 This question already has answers here : Closed 6 years ago . 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? 回答1: @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

C++ Thread taking reference argument failed compile

前提是你 提交于 2019-11-26 23:11:33
问题 #include<iostream> #include<thread> using namespace std; void f1(double& ret) { ret=5.; } int main() { double ret=0.; thread t1(f1, ret); t1.join(); cout << "ret=" << ret << endl; } The above code fails compilation with the following error message: g++ -std=c++14 -O2 -Wall -pedantic -pthread main.cpp && ./a.out In file included from /usr/local/include/c++/5.3.0/thread:39:0, from main.cpp:2: /usr/local/include/c++/5.3.0/functional: In instantiation of 'struct std::_Bind_simple<void (*(double))

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

血红的双手。 提交于 2019-11-26 20:20:41
问题 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

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

六眼飞鱼酱① 提交于 2019-11-26 18:51:52
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, from ./std_thread_refs.cpp:5: /usr/include/c++/4.8/functional: In instantiation of ‘struct std::_Bind_simple

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

て烟熏妆下的殇ゞ 提交于 2019-11-26 16:33:00
问题 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()

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

假装没事ソ 提交于 2019-11-26 15:37:27
问题 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? 回答1: 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

std::thread pass by reference calls copy constructor

冷暖自知 提交于 2019-11-26 14:40:26
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); private: //members std::ofstream fileStream; std::list<std::ostream *> listOfStreams; //disable copy

When should I use std::thread::detach?

↘锁芯ラ 提交于 2019-11-26 14:15:53
Sometime I have to use std::thread to speed up my application. I also know join() waits until a thread completes. This is easy to understand, but what's the difference between calling detach() and not calling it? I thought that without detach() , the thread's method will work using a thread independently. Not detaching: void Someclass::Somefunction() { //... std::thread t([ ] { printf("thread called without detach"); }); //some code here } Calling with detaching: void Someclass::Somefunction() { //... std::thread t([ ] { printf("thread called with detach"); }); t.detach(); //some code here }

How to check if a std::thread is still running?

好久不见. 提交于 2019-11-26 14:10:54
How can I check if a std::thread is still running (in a platform independent way)? It lacks a timed_join() method and joinable() is not meant for that. I thought of locking a mutex with a std::lock_guard in the thread and using the try_lock() method of the mutex to determine if it is still locked (the thread is running), but it seems unnecessarily complex to me. Do you know a more elegant method? Update: To be clear: I want to check if the thread cleanly exited or not. A 'hanging' thread is considered running for this purpose. Snps If you are willing to make use of C++11 std::async and std:

Thread pooling in C++11

霸气de小男生 提交于 2019-11-26 13:59:37
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> workers; int total = 4; int arr[4] = {0}; void each_thread_does(int i) { arr[i] += 2; } } int main(int argc