stdthread

C++ 11 : Start thread with member function and this as parameter

喜夏-厌秋 提交于 2019-12-05 03:02:00
问题 Using this code, I got and error : Error 1 error C2064: term does not evaluate to a function taking 1 arguments c:\program files (x86)\microsoft visual studio 11.0\vc\include\functional 1152 1 Pipeline class PipelineJob { private: std::thread *thread; void execute(PipelineJob* object); public: void execute(PipelineJob* object) { } PipelineJob() { this->thread = new std::thread(&PipelineJob::execute, this); } }; I tried many variation, any one now how to solve this? 回答1: Removing the templates

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

那年仲夏 提交于 2019-12-05 01:43:35
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, char *argv[]) ^ In file included from ../untitled/main.cpp:1: In file included from /usr/bin/../lib/c++

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

亡梦爱人 提交于 2019-12-04 03:51:28
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 TestStdThread -> Properties -> C/C++ Build -> Settings -> GCC C++ Compiler -> Includes, add /usr/include to the

Calling overloaded member functions using std::thread

强颜欢笑 提交于 2019-12-04 00:17:24
问题 Is it possible to have overloads for functions that we need to span using threads ? I have a simple class called Complex. class Complex { public: Complex():realPart_(0), imagPart_(0){} Complex(double rp, double ip) : realPart_(rp), imagPart_(ip) {} double & real() { return realPart_;} double & imag() { return imagPart_;} const double & real() const { return realPart_;} const double & imag() const { return imagPart_;} double square() const {return realPart_*realPart_ - imagPart_*imagPart_;}

std::thread <unresolved overloaded function type> error

匆匆过客 提交于 2019-12-03 22:25:02
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 (ThreadClass::*&&)(int)’ I am not sure what is the actual bug here. Can someone help me with this? Thanks

C++ 11 : Start thread with member function and this as parameter

最后都变了- 提交于 2019-12-03 20:45:00
Using this code, I got and error : Error 1 error C2064: term does not evaluate to a function taking 1 arguments c:\program files (x86)\microsoft visual studio 11.0\vc\include\functional 1152 1 Pipeline class PipelineJob { private: std::thread *thread; void execute(PipelineJob* object); public: void execute(PipelineJob* object) { } PipelineJob() { this->thread = new std::thread(&PipelineJob::execute, this); } }; I tried many variation, any one now how to solve this? Removing the templates and the pointers for simplicity, this is more or less what you would want: class PipelineJob { private: std

Efficiently waiting for all tasks in a threadpool to finish

我只是一个虾纸丫 提交于 2019-12-03 16:56:18
问题 I currently have a program with x workers in my threadpool. During the main loop y tasks are assigned to the workers to complete, but after the tasks are sent out I must wait for all tasks for finish before preceding with the program. I believe my current solution is inefficient, there must be a better way to wait for all tasks to finish but I am not sure how to go about this // called in main after all tasks are enqueued to // std::deque<std::function<void()>> tasks void ThreadPool:

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

拈花ヽ惹草 提交于 2019-12-03 10:19:52
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; std::thread worker(work, &thread_finished); while ( !thread_finished ) { // do some own work until the

Invoking a function automatically on std::thread exit in C++11

此生再无相见时 提交于 2019-12-03 06:23:19
问题 I want to set up an invocation of a function (or lambda function) to happen automatically when the current thread exits, but I cannot see any way to do it that works with std::thread unless I take over the entire task of thread creation or manually ensure that every thread calls some particular function that I always provide as its very last operation. Essentially, I want function whose prototype may resemble something like this: on_thread_exit(const std::function<void()> &func); Which would

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

旧街凉风 提交于 2019-12-03 04:41:20
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 function (and then one of the threads in the pool will be notified with a condition variable to execute