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
-2
votes
1 answer

Why is it so difficult to have a thread inside a class in c++ and invoke it with a constructor?

I have been trying to invoke threads from class constructors to no avail. Why is it so hard to pass a function or object to a thread. #include #include class a{ public: a(); std::thread t; int…
preetam
  • 1,451
  • 1
  • 17
  • 43
-2
votes
1 answer

Why mutex is not working with 2 threads sharing resource?

I want to test a scenario where I check weak_ptr for validity and return shared_ptr. between checking and returning if some other thread delete the shared_ptr we would face exception. I tried to simulate same scenario using windows sleep or cout but…
Ambarish
  • 63
  • 5
-2
votes
1 answer

Should a Thread be allocated to the stack or heap?

I'm trying to learn multithreading in C++ using the std::thread library. I can't find if a thread should be declared on the stack, or the heap; I tried searching with google and the search of this site but couldn't get a specific answer as to which…
Sotem
  • 79
  • 7
-2
votes
1 answer

How to wait for several std::threads

Based on this question: Timeout for thread.join() (ManuelAtWork's answer) I tried to implement a timeout for my std::threads: std::vector> testFlowObjects; std::thread workerThreads[MAX_PARALLEL_NR]; // maximum of 32…
leon22
  • 5,280
  • 19
  • 62
  • 100
-3
votes
1 answer

std::thread Run code in object

How can I start a thread executing code from another object/class? This is what I tried, but didn't work #import #import "Foo.h" int main() { Foo bar; std::thread asyncStuff(bar.someMethod); } So why doesn't this work, and how…
KaareZ
  • 615
  • 2
  • 10
  • 22
-4
votes
1 answer

C++ - Optimal number of threads for processing string

I have a std::string of length N and I want to insert all substrings of length K into a std::set container, using threads. How many std::thread or pthread_t objects should I use? Consider N = 500,000 and K = 3.
xennygrimmato
  • 2,646
  • 7
  • 25
  • 47
-5
votes
1 answer

C++ std::threads interacting although they should not

let me appologize in advance for being so vague about the problem. I can not share any details. It is a computer vision problem and a bunch of images are scanned and processed in main(). Every 100 (or so) frames the main() function spawns another…
pAt84
  • 213
  • 3
  • 10
-6
votes
1 answer

I was following a Udemy class but the code isnt working

C:\Users\hp\Desktop\Timer.h|77|error: no matching function for call to 'std::thread::thread(, Timer* const)'| This is the error which I get when I try to build, I was following an Udemy course, I dont have much…
Sakshat Shinde
  • 111
  • 1
  • 1
  • 7
1 2 3
39
40