stdthread

A parallel for using std::thread?

ぃ、小莉子 提交于 2019-12-01 03:04:19
问题 I'm new with std::thread and I try to code a parallel_for . I coded the following thing: // parallel_for.cpp // compilation: g++ -O3 -std=c++0x parallel_for.cpp -o parallel_for -lpthread // execution: time ./parallel_for 100 50000000 // (100: number of threads, 50000000: vector size) #include <iostream> #include <iomanip> #include <cstdlib> #include <vector> #include <thread> #include <cmath> #include <algorithm> #include <numeric> #include <utility> // Parallel for template<typename Iterator

When is it a good idea to use std::promise over the other std::thread mechanisms?

不问归期 提交于 2019-11-29 23:49:51
I am trying to establish some heuristics to help me decide the appropriate std::thread class to use. As I understand it, from highest level (simplest to use, but least flexible) to lowest level, we have: std::async with/std::future (std::shared_future) (when you want to execute on a one-time throw-away producer-thread async) std::packaged_task (when you want to assign a producer, but defer the call to the thread) std::promise (???) I think I have a decent grasp of when to use the first two, but am still unclear about std::promise . std::future in conjunction with a std::async call, effectively

Qt - emit a signal from a c++ thread

蓝咒 提交于 2019-11-29 17:22:50
问题 I want to emit a signal from a C++ thread (std::thread) in Qt. How can I do it? 回答1: You definitely can emit a signal from a thread ( QThread , std::thread or even boost::thread ). Only you must be careful of your connect function's fifth parameter ( Qt::ConnectionType ): If Qt::DirectConnection : The slot is invoked immediately (from the current thread), when the signal is emitted. If Qt::QueuedConnection : The slot is invoked when control returns to the event loop of the receiver's thread.

vector of std::threads

感情迁移 提交于 2019-11-29 12:40:41
问题 C++11 I am trying to make a vector of std::thread s. The combination of the following three points says I can. 1.) According to http://en.cppreference.com/w/cpp/thread/thread/thread, thread ’s default constructor creates a thread object which does not represent a thread. 2.) According to http://en.cppreference.com/w/cpp/thread/thread/operator%3D, thread ’s operator= Assigns the state of [the parameter, which is a thread rvalue reference] to [the calling thread] using move semantics. 3.)

using std::cout in multiple threads

≡放荡痞女 提交于 2019-11-29 06:27:24
问题 I write a simple program for testing Thread in c++11 but std::cout doesnt work as I expect. class Printer { public: void exec() { mutex m; m.lock(); cout<<"Hello "<<this_thread::get_id()<<endl; chrono::milliseconds duration( 100 ); this_thread::sleep_for( duration ); m.unlock(); } }; int main() { Printer printer; thread firstThread([&printer](){ while(1) printer.exec(); }); thread secondThread([&printer](){ while(1) printer.exec(); }); firstThread.join(); secondThread.join(); } some of the

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

不羁岁月 提交于 2019-11-29 05:15:06
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?) The C++ std::thread class is really just a minimal interface layered on top of some more complete implementation-defined threading package. As such, it only

C++ Thread taking reference argument failed compile

ⅰ亾dé卋堺 提交于 2019-11-29 02:15:49
#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))(double&)>': /usr/local/include/c++/5.3.0/thread:137:59: required from 'std::thread::thread(_Callable&

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

跟風遠走 提交于 2019-11-28 18:14:53
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 long as the function pow2 has a signature like double pow2(double); ). But the specification states "as

MinGW and std::thread

陌路散爱 提交于 2019-11-28 12:03:10
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 rubenvb MinGW-w64: stdthread experimental rubenvb 4.7 Number 1 doesn't work, since GCC apparently only

C++11: std::thread pooled?

不打扰是莪最后的温柔 提交于 2019-11-28 07:10:59
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 e.g. doesn't call pthread_create on posix), or will std::thread just be a wrapper? In other words, is