stdthread

std::thread constructor pass by reference when using a class member function

旧时模样 提交于 2019-12-11 04:37:24
问题 I've done quite a bit of research on the new c++11 rvalue and carried over lvalue. Here is a sample of what I have found and read: what-does-t-double-ampersand-mean-in-c11 how-stdthread-constructor-detects-rvalue-reference stdthread-and-rvalue-reference I have also briefed myself on rvalue references Move_Semantics rvalue_references A Proposal to Add an Rvalue Reference to the C++ Language Specifically, regarding the std::thread constructor, I found how-to-create-a-thread-inside-a-class

Thrown object cannot be caught in a multi-threaded solution

安稳与你 提交于 2019-12-11 03:03:11
问题 I have a RAII class that solves a problem in an inner thread: #include <iostream> #include <thread> using namespace std; struct solution_using_thread { solution_using_thread() : alive_(true), thread_() { thread_ = thread([this]() { while(alive_); }); } ~solution_using_thread() { alive_ = false; thread_.join(); } private: bool alive_; thread thread_; }; int main() { cout << 0 << endl; try { solution_using_thread solution; throw 1; } catch (int i ) { cout << i << endl; } cout << 2 << endl; }

Thread pool and execution queue in c++11

最后都变了- 提交于 2019-12-11 01:45:32
问题 I have a bunch of parallel tasks to complete, but only a few worker threads (say 8, but I want this to be configurable). So, 8 threads run, and each of the threads pops the next task from the queue, as long as the queue has tasks. Does C++11 provide any inbuilt constructs to help implement this design? I see some similar discussions related to std::async , but I think it leaves too much to the implementation of the compiler. 回答1: You can have std::vector<std::thread> if you want but the pool

std::thread finishes before I can detach it

廉价感情. 提交于 2019-12-11 01:44:54
问题 If I create an std::thread that terminates before I am able to call detatch() on it, what is the expected behavior? Should an exception be thrown due to the fact that joinable is already false? If this is so, then is there a way to create a thread which is initialized into a detached state so that I can avoid this error? Example: void func() { // do something very trivial so that it finishes fast int a = 1; } void main() { thread trivial(func); trivial.detach(); } I realize that it isn't

C++11 std::threads and waiting for threads to finish

折月煮酒 提交于 2019-12-11 00:48:03
问题 I have a vector of Timer Objects. Each Timer Object launches an std::thread that simulates a growing period. I am using a Command pattern. What is happening is each Timer is getting executed one after another but what I really want is for one to be executed....then once finished, the next one...once finished the next...while not interfering with the main execution of the program class Timer { public: bool _bTimerStarted; bool _bTimerCompleted; int _timerDuration; virtual ~Timer() { } virtual

C++11 threads, run on main thread

只愿长相守 提交于 2019-12-10 15:54:49
问题 I am doing some development trying out C++11 threads. I would like to run some code in an asynchronous thread and when that code is done I would like to run other code on the main thread But only when it's done! This is because the things I would like to run async is loading OpenGL stuff, and it's a bit tricky with the OpenGL contexts when doing threads, as far as I know it's pretty much a don't run the same context in different threads. However I would like to create a loader thread, which

Eclipse content assist doesn't recognize std::thread, but compiles correctly

不打扰是莪最后的温柔 提交于 2019-12-09 16:55:57
问题 I am running Ubuntu 14.04. Steps I took to reproduce: Create a new C++ project (New -> C++ -> Hello World project), which I called TestStdThread Change the code in the main file to this: #include <thread> #include <iostream> int main() { std::cout << "You have " << std::thread::hardware_concurrency() << " cores." << std::endl; return 0; } Go to TestStdThread -> Properties -> C/C++ Build -> Settings -> GCC C++ Compiler, and change the Command options from g++ to g++ -std=c++11 Go to

std::thread <unresolved overloaded function type> error

僤鯓⒐⒋嵵緔 提交于 2019-12-09 14:06:09
问题 I am trying to spawn a thread from within my class and the thread executes a particular method in my class. The code looks like this: class ThreadClass{ int myThread(int arg){ // do something } void createThread(){ thread t = thread(myThread,10); } } ; This code on compilation throws an error saying std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = int (ThreadClass::*)(int), _Args = {int}] no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘int

Is it necessary to use a std::atomic to signal that a thread has finished execution?

你。 提交于 2019-12-09 08:10:49
问题 I would like to check if a std::thread has finished execution. Searching stackoverflow I found the following question which addresses this issue. The accepted answer proposes having the worker thread set a variable right before exiting and having the main thread check this variable. Here is a minimal working example of such a solution: #include <unistd.h> #include <thread> void work( bool* signal_finished ) { sleep( 5 ); *signal_finished = true; } int main() { bool thread_finished = false;

Implementing a simple, generic thread pool in C++11

試著忘記壹切 提交于 2019-12-09 04:55:31
问题 I want to create a thread pool for experimental purposes (and for the fun factor). It should be able to process a wide variety of tasks (so I can possibly use it in later projects). In my thread pool class I'm going to need some sort of task queue. Since the Standard Library provides std::packaged_task since the C++11 standard, my queue will look like std::deque<std::packaged_task<?()> > task_queue , so the client can push std::packaged_task s into the queue via some sort of public interface