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

Detach a static const std::thread?

Is this illegal? How do I hide this global thread instance with private? It seems to work without const but I would still like to const it just for peace of mind. struct AbstractImage { private: static void LoadImages(); static const…
Anton Duzenko
  • 2,366
  • 1
  • 21
  • 26
-1
votes
1 answer

Multithreading attempt in c++- is it doing what I want?

I have attempted to make a thread pool - I have a list of data samples i want to run a function on each sample. I want to invoke all threads in order to make the total time take to process all samples quicker. The idea being each sample is done on a…
smid
  • 103
  • 1
  • 7
-1
votes
1 answer

How to make WaitForSingleObject receive a signal inside a thread called from main as a class member function?

I have a working piece of code on main function in Windows with C++ like: some_handle = dll_generate_handle; while(true) { if(WaitForSingleObject(some_handle,100)){ //CODE I AM INTERESTED ABOUT } } The signal comes from a dll…
-1
votes
1 answer

Receiving results from a thread

I am using the following code to create 10 threads. I expect to receive different random numbers from my threads and print them. but the results are the same. #include "pch.h" #include #include "C.h" #include "BB.h" #include…
omid gholami
  • 107
  • 1
  • 6
-1
votes
1 answer

Compilation error while using class member function for std::thread with unique pointer

I am new to C++, I need to use class object' member function as a thread function, and the object is used in application class and object cannot be shared so it is a unique pointer. When I am trying to create the thread, I am getting the compilation…
enthu
  • 69
  • 11
-1
votes
1 answer

How to start thread using object member function of non-copyable non-movable class?

Why does below code does not compile in Visual Studio 2015. The same code works properly if ran on ideone. class FooMutex { std::mutex stm; public: void foo() {} }; int main() { FooMutex o; std::thread t1(&FooMutex::foo,…
Daemon
  • 1,575
  • 1
  • 17
  • 37
-1
votes
2 answers

Calling threaded functions with arguments not possible

I know this question appear similar to already answered ones, but since the answer given to them does not work for me, i do not regard this question to be a dublicate of them I am well aware that the question: how do i call a c++ function as a…
Nikolaj
  • 1,137
  • 10
  • 22
-1
votes
1 answer

Exception thrown on thread creation C++ std::thread

I'm trying to spawn some threads on C++ using std::thread but I can't get it to work. If it makes any difference I'm using VS 2015. It fails on thread creation ( t[thread_id] = std::thread(test); ). This is the relevant part of my code: void test()…
-1
votes
1 answer

C++11 thread pool example with bug

Below is some code showing a simple and short implementation of a thread pool. The code is inspired by this post. I compile it with clang++ -std=c++11 threadpool.cpp -o threadpool -lpthread When executed I got following: ./threadpool terminate…
user1587451
  • 978
  • 3
  • 15
  • 30
-1
votes
1 answer

std threads with specific timeout

Currently I start my threads and wait for it to finish: void ClassA::StartTest() // btn click from GUI { ClassB classB; std::vector threads; for(int counter=0; counter<4; counter++) { threads.at(counter) =…
leon22
  • 5,280
  • 19
  • 62
  • 100
-1
votes
1 answer

std::thread error for target taking pointer args

I have a setup similar to the following, A.hpp #include class A { static void foo(char*, char*); void bar() { char* char_start = (char*) malloc(100 * sizeof(char)); char* char_end = char_start + (100 *…
AlmostSurely
  • 552
  • 9
  • 22
-1
votes
1 answer

Getting segfault using c++11 std::thread

static T MultiplyElement(const Matrix& matrixA, const Matrix& matrixB, unsigned M2col, unsigned M1row) { T sumToReturn = 0; for (unsigned iM1colM2row = 0; iM1colM2row < matrixA.m_n; iM1colM2row++) { sumToReturn += …
user2588666
  • 821
  • 1
  • 7
  • 16
-2
votes
1 answer

std::thread segfault with null native thread

Can you help me understand why this code is giving me a segfault as below? Could there be a problem in the way I create threads? I'm having difficulty understanding the issue. Please note that I have simplified the question as much as possible - I…
avatli
  • 610
  • 6
  • 16
-2
votes
1 answer

std::thread issues with compiling

The objective of this main function is to find the number of prime numbers in a range using threading to divide the problem into the selected number of threads. I'm having issues with std::thread and getting an error because of the arguments. I'm…
-2
votes
1 answer

Guarded thread C++

I'm trying to create a "guarded_thread", but I receive an error 'operator=' is a private member of 'std::__1::thread'. Here is my code: struct guarded_thread : std::thread{ using std::thread::thread; using std::thread::operator=; …
N.T.
  • 1
  • 1
1 2 3
39
40