stdthread

Correct way to pause & resume an std::thread

て烟熏妆下的殇ゞ 提交于 2019-12-19 06:43:43
问题 I am using an std::thread in my C++ code to constantly poll for some data & add it to a buffer. I use a C++ lambda to start the thread like this: StartMyThread() { thread_running = true; the_thread = std::thread { [this] { while(thread_running) { GetData(); } }}; } thread_running is an atomic<bool> declared in class header. Here is my GetData function: GetData() { //Some heavy logic which needs to be executed in a worker thread } Next I also have a StopMyThread function where I set thread

How to stop an std::thread from running, without terminating the program

坚强是说给别人听的谎言 提交于 2019-12-18 04:24:12
问题 I am trying to learn std::threads from C++11 to make a threading system. I was wondering if there is a way to stop a thread from running (Not sleeping, but really destructing the thread or so to speak) without terminating the whole program. I know std::join exists, but that forces a thread to wait till all threads return. Is there another way to handle this? (For example for making a ThreadPool class without having to block a thread?) 回答1: The C++ std::thread class is really just a minimal

C++11: std::thread pooled?

风流意气都作罢 提交于 2019-12-17 15:46:43
问题 In C++03 I used pthread with a self-built thread pool that always kept a couple of threads running (since pthread_create is slow), this way I was able to launch threads for small tasks without thinking about performance issues. Now, in C++11 we have std::thread . I guess the standard doesn't say anything about the specific implementation, so my question is about the standard library implementations. Are they generally opting for a pooled approach where constructing std::thread s is cheap (and

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

隐身守侯 提交于 2019-12-17 02:33:50
问题 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(

Issue with std::thread when using g++ in 32-bit MinGW 4.8.0

限于喜欢 提交于 2019-12-13 15:27:51
问题 BACKGROUND -- We develop C++11 code and write unit tests using gtest/gmock. This is built on a Windows server using SCons and g++ in MinGW . We started having occasional problems when executing unit tests: silent exits, expectation errors, exception pop-ups... with no obvious pattern or commonality and not easily reproduceable. Eventually, a colleague narrowed it down to a case when apparently a thread was joined without even starting to execute its payload function. In this case, there were

Different behavior when `std::lock_guard<std::mutex>` object has no name

女生的网名这么多〃 提交于 2019-12-12 10:45:46
问题 I'm learning about std::mutex , std::thread and I am surprised at the different behavior of 2 pieces of code below: #include <iostream> #include <mutex> #include <thread> using namespace std; std::mutex mtx; void foo(int k) { std::lock_guard<std::mutex> lg{ mtx }; for (int i = 0; i < 10; ++i) cout << "This is a test!" << i << endl; cout << "The test " << k << " has been finished." << endl; } int main() { std::thread t1(foo, 1); std::thread t2(foo, 2); t1.join(); t2.join(); return 0; } The

Calling threaded functions with arguments not possible

倖福魔咒の 提交于 2019-12-12 06:49:22
问题 I know this question appear similar to already answered ones, but since the answer given to them does not work for me, i do not regard this question to be a dublicate of them I am well aware that the question: how do i call a c++ function as a thread which has 1 or more arguments has been answered several times -- both here, and on various tutorials -- and in every case the answer is simply that this is the way to do it: (example taken directly from this question) #include <string> #include

std::thread works in cygwin but not in MinGw

℡╲_俬逩灬. 提交于 2019-12-12 04:17:38
问题 So I decided to give c++ a try today. I downloaded MinGw and the g++ compiler that comes with it. I decided to test the following code: #include <iostream> #include <thread> int foo() { std::cout << "foo" << std::endl; } int main() { std::thread t1(foo); t1.join(); std::cout << "done" << std::endl; return 0; } I then tried to compile it on the command line using the following line: g++ -std=c++11 main.cpp Which works for hello world. This time however, it gave me this error: error: 'thread'

Can I use `std::this_thread::sleep_for()` with an MPI process?

纵饮孤独 提交于 2019-12-11 13:27:13
问题 MPI runs my program with multiple processes. I'd one of these processes to sleep for a while so that it's using minimal CPU. std::this_thread::sleep_for() looks like what I want, but that thread bit looks a little sketchy in this context. Is it okay to do this? 回答1: This is perfectly OK to do - nothing should crash or hang as a result of it. However, your "so that it's using minimal CPU" is a little worrying. Are you running more MPI processes than you have hardware threads available to

Error C2280 while creating a std::thread without initialization in C++

妖精的绣舞 提交于 2019-12-11 04:51:59
问题 I want to create a thread object in my class/struct but don't want to initialie immediately during declaration. Intead want to do it later. Hence, I did as in below code snippet, I'm getting this compilation error. How do I fix this ? error C2280: 'std::thread::thread(const std::thread &)' : attempting to reference a deleted function I observed the line causing this error is : std::thread t2; Does it mean that we can't just declare a std::thread without initializing ? This contradicts the