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
0
votes
2 answers

using clr and std::thread

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…
JMRC
  • 1,473
  • 1
  • 17
  • 36
0
votes
1 answer

Follow-up: Executing a member function of a class

This is a follow-up for the problem I posted earlier at Executing a member function of a class. I am trying to experiment the C++1.1 threads in a way that it accepts a member function of a class as a parameter into the thread constructor as shown…
F. Aydemir
  • 2,665
  • 5
  • 40
  • 60
0
votes
1 answer

What may cause a thread id to be -1?

I've currently been playing around with concurrent objects, based on Herb Sutter's presentation. I'm currently using Visual Studio 2012 with November CTP (could not check the stuff below in another way, Clang doesn't like class members in decltypes,…
DorianGrey
  • 45
  • 5
0
votes
1 answer

How to let this_thread::sleep_for(a while)?

Possible Duplicate: std::this_thread::sleep_for() and GCC Tried to write a simple timer and some code to test it by letting the program sleep for some time: #include #include Aux::Timer…
clstaudt
  • 21,436
  • 45
  • 156
  • 239
0
votes
0 answers

Parallel for: not the same result on my computer and ideone?

Possible Duplicate: A parallel for using std::thread? Consider the following code that implements a parallel_for: // parallel_for.cpp // compilation: g++ -O3 -std=c++0x parallel_for.cpp -o parallel_for -lpthread // execution: time ./parallel_for…
Vincent
  • 57,703
  • 61
  • 205
  • 388
0
votes
2 answers

asio .async_* won't run unless main thread calls io_service.run

I've recently been playing around with boost asio and some new c++11 constructs. Here is a sample section of code that causes unexpected behavior (for me at least). void Server::startAccept() { …
flumpb
  • 1,707
  • 3
  • 16
  • 33
0
votes
2 answers

Two identical (network) calls. Howto wait for fastest, and discard slowest?

In c++11 how would one go about implementing a program that does two expensive (network) calls, of the same type, and then only waits for the result from the quicker one, not waiting and discarding the slower result. std::thread cant be interrupted…
petke
  • 123
  • 1
  • 7
0
votes
3 answers

is this code safe , is it ok to spawn a thread from a constructor C++?

I have a requirement to embed a thread inside a C++ class, kind of active object but not exactly. i am spawning thread from the constructor of the class , is it ok to do this way are there any problems with this approach. #include…
Ravikumar Tulugu
  • 1,702
  • 2
  • 18
  • 40
-1
votes
1 answer

Compiler error: "invoke: No matching overloaded function found" when creating a new thread

I have the following C++ code: #include #include #include #include #include void ThreadFunc(std::vector* pIntVector, std::mutex* pMtx, int i, int j) { std::lock_guard lock(*pMtx); …
-1
votes
3 answers

Why do I get "Segmentation fault (core dumped)" error when trying to implement multithreading in c++?

I have a main file where I plan to initiate the threads for my c++ program, for now, I only want to get one of the threads up and running before moving on to the others, but that is proving to be difficult. The purpose of the threads is for a TCP…
-1
votes
1 answer

std::thread dosnt dosnt excute funtion c++

i have this simple function : class Timer { std::atomic active{true}; public: void setInterval(auto function, int interval); void stop(); }; void Timer::setInterval(auto function, int interval) { …
user63898
  • 29,839
  • 85
  • 272
  • 514
-1
votes
1 answer

How to wait for a thread to finish without high CPU load?

I have a program that creates multiple working threads and then wait's for them to finish. The code is equivalent to this: The worker functions both contain infinite loops and do not finish during normal operation. void workerFunction1(void) { …
Zciurus
  • 786
  • 4
  • 23
-1
votes
1 answer

Why `std::thread()` and `std::packaged_task()` works different, although they both accept callable targets?

Here is a simple code snippet: #include #include #include void foo(int){} int main() { std::thread(foo, 1).join(); //works indeed std::packaged_task task{foo, 1}; //complian …
John
  • 2,963
  • 11
  • 33
-1
votes
1 answer

About the parameters passed to the ctor of `std::thread`

As this code snippet does not compile, I understand that the std::thread need callable&& other than callable&. #include #include #include #include #include // unique…
John
  • 2,963
  • 11
  • 33
-1
votes
2 answers

What is the correct way of freeing std::thread* heap allocated memory?

I'm declaring a pointer to a thread in my class. class A{ std::thread* m_pThread; bool StartThread(); UINT DisableThread(); } Here is how I call a function using a thread. bool A::StartThread() { bool mThreadSuccess = false; …