stdthread

C++ 11 std::thread strange behavior

风格不统一 提交于 2019-12-24 01:19:08
问题 I am experimenting a bit with std::thread and C++11, and I am encountering strange behaviour. Please have a look at the following code: #include <cstdlib> #include <thread> #include <vector> #include <iostream> void thread_sum_up(const size_t n, size_t& count) { size_t i; for (i = 0; i < n; ++i); count = i; } class A { public: A(const size_t x) : x_(x) {} size_t sum_up(const size_t num_threads) const { size_t i; std::vector<std::thread> threads; std::vector<size_t> data_vector; for (i = 0; i

Can't pass parameters to std::thread?

≯℡__Kan透↙ 提交于 2019-12-23 20:41:33
问题 I'm trying to use std::thread . My thread is supposed to call a method and pass a struct as a parameter, as so many examples show. Except my very simple code won't compile. For the record, I'm aware of this question but nothing there seems to help me. Where I call the thread: void Exporter::save() const { thread(write_to_disk, this->parameter).detach(); } The signature of write_to_disk : void write_to_disk(const Parameter& parameter) write_to_disk is defined in a nameless namespace in the

'thread' is not a member of 'std' in GCC 4.8

[亡魂溺海] 提交于 2019-12-23 19:28:29
问题 I'm trying out some c++11 threading support for my current project but facing bottleneck. I use the gcc 4.8.0 built by rubenvb (downloaded directly from sourceforge) [x86_64-w64-mingw32-gcc-4.8.0-win64_rubenvb] and find it seems not support c++11 threading features. The following code fails to compile and complains 'thread' is not a member of 'std' . With googling, there is a 4.7-experimental built (also by rubenvb) with std thread support. As part of my main project requires other C++11

Approach of using an std::atomic compared to std::condition_variable wrt pausing & resuming an std::thread in C++

☆樱花仙子☆ 提交于 2019-12-23 09:25:30
问题 This is a separate question but related to the previous question I asked here 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 } Next I also have a StopMyThread

Threading opencl compiling

﹥>﹥吖頭↗ 提交于 2019-12-23 08:36:10
问题 [Update:] I'm spawning multiple processes now and it works fairly well, though the basic threading problem still exists. [/] I'm trying to thread a c++ (g++ 4.6.1) program that compiles a bunch of opencl kernels. Most of the time taken is spent inside clBuildProgram. (It's genetic programming and actually running the code and evaluating fitness is much much faster.) I'm trying to thread the compilation of these kernels and not having any luck so far. At this point, there's no shared data

Threading opencl compiling

落爺英雄遲暮 提交于 2019-12-23 08:36:01
问题 [Update:] I'm spawning multiple processes now and it works fairly well, though the basic threading problem still exists. [/] I'm trying to thread a c++ (g++ 4.6.1) program that compiles a bunch of opencl kernels. Most of the time taken is spent inside clBuildProgram. (It's genetic programming and actually running the code and evaluating fitness is much much faster.) I'm trying to thread the compilation of these kernels and not having any luck so far. At this point, there's no shared data

How can I declare an std::thread anonymously?

一笑奈何 提交于 2019-12-23 07:26:20
问题 Consider the following short program: #include <thread> int Foo() { while (1); } int main(){ std::thread t(Foo); std::thread s(Foo); // (std::thread(Foo)); t.join(); } This compiles and runs (forever), with g++ -Wl,--no-as-needed DoubleBufferTest.cc -o DoubleBufferTest -std=c++0x -pthread In the commented out line, I am trying to use the technique described here to declare a new thread anonymously. However, when that line is commented back in, I can compile but running gives the following

Is it possible to define an std::thread and initialize it later?

时光毁灭记忆、已成空白 提交于 2019-12-21 07:14:08
问题 My aim is to keep an std::thread object as data member, and initialize it when needed. I'm not able to do this (as in my code below) because the copy constructor of the std::thread class is deleted. Is there any other way to do it? class MyClass { public: MyClass():DiskJobThread(){}; ~MyClass(); void DoDiskJobThread(); private: int CopyThread(const std::wstring & Source, const std::wstring & Target); int MoveThread(const std::wstring & Source, const std::wstring & Target); std::thread

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

穿精又带淫゛_ 提交于 2019-12-20 12:36:15
问题 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

While loop in main thread is getting stuck when using std::thread

大憨熊 提交于 2019-12-19 10:58:24
问题 I have a simple C++ code to test and understand threading. The code has the main thread + a secondary thread. The secondary updates the value of a variable which the main thread loop depends on. When I add a print statement inside the main loop the program finishes successfully, but when I remove this print statement it goes into an infinite loop. This is the code that I'm using, and the print statement that I'm referring to is print statement 2 #include <mpi.h> #include <iostream> #include