stdthread

Non-obvious lifetime issue with std::promise and std::future

青春壹個敷衍的年華 提交于 2019-12-03 02:50:55
This question is very similar to a previous one here: race-condition in pthread_once()? It is essentially the same issue - the lifetime of a std::promise ending during a call to promise::set_value (ie: after the associated future has been flagged, but before pthread_once has executed) So I know that my usage has this issue, and that I therefore cannot use it in this way. However, I think this is non-obvious. (In the wise words of Scott Meyer: Make Interfaces Easy to Use Correctly and Hard to Use Incorrectly ) I present an exemplar below: I have a thread ( dispatcher ) which spins on a queue,

using clr and std::thread

杀马特。学长 韩版系。学妹 提交于 2019-12-02 10:18:32
I'm creating an UI abstraction layer for desktops. Now I'm implementing the functionality of the .NET framework. The annoying thing is that if I let the users create a CLR Windows Forms Application in Visual studio they can't use all the standard libraries like std::thread and if I let them create another type of application, the console shows up. Is there a way to use clr with std::thread or, even better, is there a way to prevent the console from starting (or hide it from both the screen and the taskbar)with a CLR Console or CLR Empty project. Thanks This is an old question, but in case

Deleting std::thread pointer raises exception “libc++abi.dylib: terminating”

馋奶兔 提交于 2019-12-01 20:10:08
In C++ 11 with LLVM 6.0 on Mac OS X, I first created a pointer to a memory allocation of std::thread. std::thread* th = new std::thread([&] (int tid) { // do nothing. }, 0); Then I tried to delete it. delete th; However, compiling the above code and execute it raises exception libc++abi.dylib: terminating Abort trap: 6 The thread you've created is joinable , and unless you join or detach it, std::terminate will be called when the destructor of the thread object executes. So you need th->join(); delete th; Early proposals for std::thread implicitly detach ed the thread in the destructor, but

Deleting std::thread pointer raises exception “libc++abi.dylib: terminating”

断了今生、忘了曾经 提交于 2019-12-01 19:03:14
问题 In C++ 11 with LLVM 6.0 on Mac OS X, I first created a pointer to a memory allocation of std::thread. std::thread* th = new std::thread([&] (int tid) { // do nothing. }, 0); Then I tried to delete it. delete th; However, compiling the above code and execute it raises exception libc++abi.dylib: terminating Abort trap: 6 回答1: The thread you've created is joinable, and unless you join or detach it, std::terminate will be called when the destructor of the thread object executes. So you need th-

C++ std::vector of independent std::threads

自闭症网瘾萝莉.ら 提交于 2019-12-01 17:00:13
I´m building a real time software where I have a main infinite loops on main() and threads used to read and process data. One of the issues is keeping a std::vector of running threads to send signals to them and to monitor execution. So I put together this code: #include <iostream> #include <string> #include <vector> #include <thread> #include <chrono> namespace readerThread { void start(int id) { while (1) { std::cout << "Reader " << id << " running..." << std::endl; std::this_thread::sleep_for(std::chrono::milliseconds(1000)); } } } int main() { int readers[] = { 1, 2, 3 }; std::vector<std:

C++ std::vector of independent std::threads

ぃ、小莉子 提交于 2019-12-01 14:38:27
问题 I´m building a real time software where I have a main infinite loops on main() and threads used to read and process data. One of the issues is keeping a std::vector of running threads to send signals to them and to monitor execution. So I put together this code: #include <iostream> #include <string> #include <vector> #include <thread> #include <chrono> namespace readerThread { void start(int id) { while (1) { std::cout << "Reader " << id << " running..." << std::endl; std::this_thread::sleep

learning threads on linux

你离开我真会死。 提交于 2019-12-01 08:49:27
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. Ultimately, std::thread seems more important, but there's a lot of legacy code out there, so I'll have

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

只愿长相守 提交于 2019-12-01 08:36:25
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? The description on cppreference suggests that they are passed as l-values (they are given names

A parallel for using std::thread?

最后都变了- 提交于 2019-12-01 05:43:27
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, class Function> void parallel_for(const Iterator& first, const Iterator& last, Function&& f, const

Calling overloaded member functions using std::thread

烈酒焚心 提交于 2019-12-01 03:40:26
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_;} void display() const { std::cout << "Square of the Complex number (" << realPart_ << ") + i (" <<