stdthread

Issue with std::thread from c++11

亡梦爱人 提交于 2021-02-04 05:54:11
问题 I have some troubles trying to compile a program with multi-threading from the standard template library. It return me a obscure error when i try to compile the following program : #include <iostream> #include <thread> void foo() { std::cout << "Thread 1\n"; } int main(int argc, char** argv) { std::thread tr(foo); std::cout << "Main thread\n"; tr.join(); return 0; } I don't understand the error : /tmp/ccE8EtL1.o : In the function « std::thread::thread<void (&)()>(void (&)()) » : file.cpp:(

Issue with std::thread from c++11

﹥>﹥吖頭↗ 提交于 2021-02-04 05:51:57
问题 I have some troubles trying to compile a program with multi-threading from the standard template library. It return me a obscure error when i try to compile the following program : #include <iostream> #include <thread> void foo() { std::cout << "Thread 1\n"; } int main(int argc, char** argv) { std::thread tr(foo); std::cout << "Main thread\n"; tr.join(); return 0; } I don't understand the error : /tmp/ccE8EtL1.o : In the function « std::thread::thread<void (&)()>(void (&)()) » : file.cpp:(

std::thread increses DLL reference count, which prevents the unloading of the DLL

北城以北 提交于 2021-01-29 04:35:01
问题 I have a windows c++ dll which gets loaded by a third party program. I recently added a thread pool (this one https://github.com/progschj/ThreadPool/blob/master/ThreadPool.h). But now the dll get no longer unloaded when the third party program no longer needs it. The reason is that every thread spawned in the thread pool increases the dll reference count by one. One problem is that I don't know when the third party program no longer needs the dll, so I can't manually shutdown the thread pool

Can one retrieve the return value of a thread function in C++11?

跟風遠走 提交于 2020-12-06 06:58:05
问题 If a function has a non-void return value and I join it using the .join function then is there any way to retrieve its return value? Here is a simplified example: float myfunc(int k) { return exp(k); } int main() { std::thread th=std::thread(myfunc, 10); th.join(); //Where is the return value? } 回答1: You can follow this sample code to get the return value from a thread :- int main() { auto future = std::async(func_1, 2); //More code later int number = future.get(); //Whole program waits for

using std::thread in a library loaded with dlopen leads to a sigsev

家住魔仙堡 提交于 2020-07-15 04:42:09
问题 I recently discovered a strange behaviour using std::thread and dlopen . Basically, when I execute a std::thread in a library which is loaded using dlopen I receive a sigsev. The library itself is linked against pthread, the executable that calls dlopen is not. Once I link the executable against pthread or the library itself everything works fine. However, we are using a plugin based infrastructure, where we do not know if the application itself is linked against pthread or not. Therefore, it

Storing an std::thread in C++11 smart pointer

拥有回忆 提交于 2020-07-05 07:57:13
问题 In C++ 11 & above what are the advantages or disadvantages when storing an std::thread as a member of class directly like so: std::thread my_thread; As opposed to storing a std::shared_ptr or std::unique_ptr to the thread like so: std::shared_ptr<std::thread> my_thread_ptr; Is any of the code options better than other? Or it doesn't matter, just 2 separate ways of handling the thread object. 回答1: May be there is some less common reason for usage of pointer (or smart pointer) member but for

C++11 on modern Intel: am I crazy or are non-atomic aligned 64-bit load/store actually atomic?

血红的双手。 提交于 2020-05-16 08:04:33
问题 Can I base a mission-critical application on the results of this test, that 100 threads reading a pointer set a billion times by a main thread never see a tear? Any other potential problems doing this besides tearing? Here's a stand-alone demo that compiles with g++ -g tear.cxx -o tear -pthread . #include <atomic> #include <thread> #include <vector> using namespace std; void* pvTearTest; atomic<int> iTears( 0 ); void TearTest( void ) { while (1) { void* pv = (void*) pvTearTest; intptr_t i =

C++11 on modern Intel: am I crazy or are non-atomic aligned 64-bit load/store actually atomic?

倖福魔咒の 提交于 2020-05-16 08:04:21
问题 Can I base a mission-critical application on the results of this test, that 100 threads reading a pointer set a billion times by a main thread never see a tear? Any other potential problems doing this besides tearing? Here's a stand-alone demo that compiles with g++ -g tear.cxx -o tear -pthread . #include <atomic> #include <thread> #include <vector> using namespace std; void* pvTearTest; atomic<int> iTears( 0 ); void TearTest( void ) { while (1) { void* pv = (void*) pvTearTest; intptr_t i =