Questions tagged [stdthread]

std::thread is a C++11 standard library class which represents a single thread of execution. Threads allow multiple functions to execute concurrently.

std::thread is a C++11 standard library type which creates a new thread and runs a function (or other callable object) in that new thread.

Include the <thread> header to use std::thread.

C++20 added std::jthread which automatically .join()s the thread on destruction instead of std::terminate()ing, a generally preferable behavior.

593 questions
4
votes
0 answers

compile problems in c++ concurrency in action listing 9.9(msvc 12.0)

I am trying to compile this on msvc 12.0, the code in the book uses the keyword thread_local but it seems msvc 12.0 does not support this? Instead of using thread_local I tried __declspec(thread) but I get the following compiler error: Error 1 …
4
votes
1 answer

C++11 thread::id special value that represent no thread

This man page states that Instances of this class may also hold the special distinct value that does not represent any thread. But I couldn't find nowhere what would be this special value or where it is defined (looked in < thread >). Any ideas?
CodeMonkey1
  • 123
  • 1
  • 6
4
votes
1 answer

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

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…
Strin
  • 677
  • 9
  • 15
4
votes
1 answer

Nested openMP parallelisation in combination with std::thread

Hello fellow StackOverFlowers, I am currently working on a bigger project in the area of image-processing. I am developing using Visual Studio 2013 (not negotiable). Without bothering you with any further details, here is my problem: I have two…
LorToso
  • 286
  • 1
  • 9
4
votes
3 answers

Creating an instance of shared_ptr with make_shared

Consider the following code: class A { .... shared_ptr mThread; void Step(); void LaunchTrhead(); } void A::LaunchThread() { ... mThread=make_shared(Step); // This line gives an error …
DrD
  • 419
  • 4
  • 14
4
votes
2 answers

Waiting for a std::thread to finish

I am trying to clean up gracefully on program termination, so I'm calling join() on a std::thread to wait for it to finish. This simply seems to block the main thread forever, but I don't understand why, because the worker thread is an (almost)…
Kristian D'Amato
  • 3,996
  • 9
  • 45
  • 69
4
votes
3 answers

How to spawn multiple threads that call same function using std::thread C++

Basically what I would like to do is write a for loop that spawns multiple threads. The threads must call a certain function multiple times. So in other words I need each thread to call the same function on different objects. How can I do this using…
user2481095
  • 2,024
  • 7
  • 22
  • 32
4
votes
2 answers

Trouble with multiple std::threads and main program execution

I have been struggling for days to come up with a mechanism for launching a few timers and not having it clock the main program execution. Combinations of .join() and .detach(), wait_until(), etc What I have is a vector of std::thread and I want…
Jasmine
  • 15,375
  • 10
  • 30
  • 48
4
votes
1 answer

C++11 threads, run on main thread

I am doing some development trying out C++11 threads. I would like to run some code in an asynchronous thread and when that code is done I would like to run other code on the main thread But only when it's done! This is because the things I would…
qrikko
  • 2,483
  • 2
  • 22
  • 35
4
votes
1 answer

Is this a BIG bug of Microsoft's implementation of std::thread?

#define _CRTDBG_MAP_ALLOC #include #include #include using namespace std; void Hello() {} int main() { { std::thread(Hello).join(); } _CrtDumpMemoryLeaks(); } The output windows shows:…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
4
votes
1 answer

Why this program throws 'std::system_error'?

Possible Duplicate: Why does this simple std::thread example not work? Code: #include #include void f() { std::cout << "hi thread" << std::endl; } int main() { std::thread t(f); std::cout << "hi" << std::endl; …
lvella
  • 12,754
  • 11
  • 54
  • 106
4
votes
2 answers

instant segmentation fault with debian and std::thread c++

got this problem - in title.. I have this code: #include #include void my_thread_func() { std::cout<<"hello"<
Wiciu
  • 125
  • 1
  • 7
4
votes
5 answers

learning threads on linux

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…
user206705
3
votes
1 answer

How should I wait for thread to finish with a timeout using C++11?

I have Windows multi-threaded code that I'm trying to make portable by using the C++11 thread and synchronization classes. How should the main thread wait for a worker thread with a timeout? I tried using a condition variable, but it's possible for…
3
votes
1 answer

How to handle a PostMessageThread message in std::thread?

Somewhere in my main thread I am calling PostThreadMessage(). But I don't know how to handle it in the std::thread I have sent it to. I am trying to handle it in std::thread like this: while(true) { if(GetMessage(&msg, NULL, 0, 0)) { //…