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

Error when creating thread using a class as a parameter and passing data using the class constructor

I'm 'modernizing' some piece of code to c++11 'standards' (std::thread etc), I'm trying to spawn a thread using std::thread and passing a class as a parameter but I can't get it to work for some reason.. Class definition: Class…
0
votes
1 answer

Moving an object to a thread with std::threads?

I have a basic producer/consumer application where some workers go and perform a task on a shared problem. It's not trivial enough to use a concurrent for or something similar. I've gotten used to using Qt for threading because it makes it very…
Josh
  • 2,658
  • 31
  • 36
0
votes
1 answer

passing a reference to virtual class implementation as thread argument

Once again I need your help making std::thread work with templated objects. This time my issue is in one of the arguments. I try passing a reference to a (valid) instantiation of a virtual class as one of the arguments of my thread, and it just…
Amxx
  • 3,020
  • 2
  • 24
  • 45
0
votes
0 answers

launch a thread running an inherited templated method

I'm trying to have a class launch a inherited templated methode into multiple thread. The basic idea is to have a class A be able to do work sequentially, and have a class B be able to run parallele execution of A (with custom arguments) to do the…
Amxx
  • 3,020
  • 2
  • 24
  • 45
0
votes
1 answer

Accessing a running Thread

I am writing a plugin that uses a GLEWMX context. The plugin loads the GLEWMX context under thread id (1). Then when I got to run something it runs that task under thread id (2). I have access to both thread ids. Should I switch the active thread to…
0
votes
1 answer

Basic thread leads to a malloc(): memory corruption:

I've got a class with a simple thread pointer, that I use to start a function in a new thread from my constructor. class Tty { public: Tty(); private: void foo(); std::thread tFoo; }; using namespace std; Tty::Tty() { tFoo =…
Antoine C.
  • 3,730
  • 5
  • 32
  • 56
0
votes
1 answer

std::thread blocks in a singleton and .dll

I've read a lot about this, but I haven't found any solution yet. My problem is that when I try to do this, I loose control. hiloEscucha = std::thread(&PlayerClientSingleton::Run, this); I'm working with a class implementing the singleton pattern in…
jromeror
  • 100
  • 12
0
votes
0 answers

Join a non-child thread

Normally you call join() on a thread from it's parent to wait for it to finish. Is it allowed for a thread to call join() on another thread that is not it's child? And if it is, should this ever be used?
Catalin
  • 87
  • 2
  • 6
0
votes
2 answers

Parallel implemention of Lisp-style mapping of a function to a list in C++ fails without cout after use of thread

This code works only when any of the lines under /* debug messages */ are uncommented. Or if the list being mapped to is less than 30 elements. func_map is a linear implementation of a Lisp-style mapping and can be assumed to work. Use of it would…
IjRUN
  • 3
  • 2
0
votes
0 answers

Is there anyway to "Intercept" an std::thread?

Is there anyway to interrupt an std::thread while it's running, call a new method with it, have it return to where it was, and still have it be valid to use with the opengl context, windows etc that it has created?
Ben
  • 1,816
  • 1
  • 20
  • 29
0
votes
1 answer

C++ std::thread invalid use of void expression

I'm having an issue with my threading program. I know what the issue is, I just don't know how to fix it. I'm setting up an arbitrary number of threads to create a mandelbrot set and then to write it out to a ppm file. I'm using a vector of…
Josh Davis
  • 157
  • 3
  • 10
0
votes
2 answers

How frequently does mutex::lock() check for the unlocked state if it's already locked by another thread?

According to cppreference, constructing an std::lock_guard with a std::mutex parameter calls the lock() method of that mutex. According to cplusplus, regarding mutex’s lock() method: If the mutex is locked by another thread, execution of the…
CodeBricks
  • 1,771
  • 3
  • 17
  • 37
0
votes
1 answer

std::thread constructor (amount of variables)

Hello i have a little problem with my program (i want to multiply array by scalar). Basicly i want to create a vector of threads that will do the multiplication staff (element by element) Code samples First mainImplementation function void…
tezaja
  • 13
  • 4
0
votes
1 answer

Constructing object in new thread?

I'm trying to construct objects, that take a while to build in a seperate thread. (Later for loading game assets in real time, while the render loop is running :)) While the object is still building, requests to that object will not fail, but do…
haansn08
  • 157
  • 3
  • 10
0
votes
0 answers

thread lambda function wont compile

I'm struggling to understand why this happen, maybe it's a bug. #include #include using namespace std; int main(){ int y=2; float fa[2][y]; // thread compile fine if y were 2 hardcoded instead fa[0][0]=0.8; …
superbem
  • 441
  • 3
  • 10