stdthread

learning threads on linux

て烟熏妆下的殇ゞ 提交于 2019-12-30 10:38:42
问题 Linux is a new platform to me. I've coded on Windows in c++ for a number of years and have become comfortable with multithreading on that platform. Along comes C++11 at a time when I need to learn c++ on the linux platform. Linux appears to use pthreads for the most part - okay there's also boost::threads and QT have their own threads too. But with C++11 comes std::thread, a whole new (cross platform and C++ standard) way to do threads. So I guess I'll have to learn pthreads and std::threads.

Details in the process of constructing a std::thread object

无人久伴 提交于 2019-12-30 10:35:27
问题 I'm interested in (and confused about) the details of constructing a std::thread object. According to cppreference, both the thread function and all arguments are value-copied to some thread-accessible storage, and then invoke. 1) What exactly is this thread-accessible storage? Is it semantically equivalent to some kind of thread-local storage, and the variables are destructed after the thread function returned? 2) What is the value-category of the arguments when passed to the thread function

Details in the process of constructing a std::thread object

China☆狼群 提交于 2019-12-30 10:35:05
问题 I'm interested in (and confused about) the details of constructing a std::thread object. According to cppreference, both the thread function and all arguments are value-copied to some thread-accessible storage, and then invoke. 1) What exactly is this thread-accessible storage? Is it semantically equivalent to some kind of thread-local storage, and the variables are destructed after the thread function returned? 2) What is the value-category of the arguments when passed to the thread function

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

亡梦爱人 提交于 2019-12-29 14:20:55
问题 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,

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

倾然丶 夕夏残阳落幕 提交于 2019-12-27 10:59:10
问题 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

Array of threads and attempting to pass multiple arguments to function is not working?

孤人 提交于 2019-12-25 14:07:46
问题 I am creating a program with a dynamic number of threads. I have a vector for the threads (thanks, Mohamad); then I attempt to call a function and pass multiple arguments for the thread of execution. However, my current code gives an error which I assume is due to my odd usage of the 2D array: In function 'int main()': 102 77 [Error] no matching function for call to 'std::thread::thread(void (&)(float ( )[2], float , int, int), float [(((sizetype)(((ssizetype)nodeNumber) + -1)) + 1)][2],

Array of threads and attempting to pass multiple arguments to function is not working?

北慕城南 提交于 2019-12-25 14:07:34
问题 I am creating a program with a dynamic number of threads. I have a vector for the threads (thanks, Mohamad); then I attempt to call a function and pass multiple arguments for the thread of execution. However, my current code gives an error which I assume is due to my odd usage of the 2D array: In function 'int main()': 102 77 [Error] no matching function for call to 'std::thread::thread(void (&)(float ( )[2], float , int, int), float [(((sizetype)(((ssizetype)nodeNumber) + -1)) + 1)][2],

Is it a good practice to call pthread_sigmask in a thread created by std::thread?

烂漫一生 提交于 2019-12-24 03:30:01
问题 1) I'm new to std::thread and I would like to know whether it is a good practice to call pthread_sigmask() to block some signals in a particular thread created by std::thread . I don't want the new thread to receive signals such as SIGTERM, SIGHUP, etc., because the main process has already installed handlers for these signals. So, is it a good practice to call pthread_sigmask() to block some signals in a thread created by std::thread ? 2) Also, I believe the effect of pthread_sigmask(SIG

Implementing a function that perfect-forwards to std::thread

丶灬走出姿态 提交于 2019-12-24 02:33:44
问题 I am trying to write a wrapper around std::thread : #include <thread> #include <iostream> struct A {}; template <typename F, typename... Args> void lifted_lambda_1(void *m, F &&entrypoint, Args&&... args) { std::cout << "I will do something with the void * " << m << std::endl; entrypoint(std::forward<Args>(args)...); } template <typename F, typename... Args> void make_thread(void *p, F &&f, Args && ... args) { std::thread(lifted_lambda_1<typename std::decay<F>::type, Args...>, p, std::forward

Implementing a function that perfect-forwards to std::thread

孤者浪人 提交于 2019-12-24 02:33:25
问题 I am trying to write a wrapper around std::thread : #include <thread> #include <iostream> struct A {}; template <typename F, typename... Args> void lifted_lambda_1(void *m, F &&entrypoint, Args&&... args) { std::cout << "I will do something with the void * " << m << std::endl; entrypoint(std::forward<Args>(args)...); } template <typename F, typename... Args> void make_thread(void *p, F &&f, Args && ... args) { std::thread(lifted_lambda_1<typename std::decay<F>::type, Args...>, p, std::forward