Storing a Std :: thread in a C ++ Smart Pointer 11

In C++ 11 & above what are the advantages or disadvantages when storing an std::thread as a member of class directly like so: std::thread my_thread; As opposed to storing a std::shared_ptr or std::unique_ptr to the thread like so: std::shared_ptr<s

blocks the main thread to wait for his sons

So I have this class: class foo { public: foo() { }; void me1() const { while(1) { std::lock_guard<std::mutex> ldock(m); std::cout << 0; } } void me2() const { while(1) { std::lock_guard<std::mutex> ldock(m); std::cout << 1; } } pr

How to join a std :: thread from itself (in C ++ 11)

I have a std::thread waiting and reading from a socket. And there is a pointer to this thread stored somewhere. But when something bad happens and the thread ends, I want it to call something that results in a function that will join this thread and

No match for call when launching thread on constructor

I have a class Foo, that must launch a thread (which is a method loop() on my class) on the constructor. My class is something like this on the header file: class Foo { public: Foo(); ~Foo(); private void loop(); std::thread m_thread; //more stuff. }

Implementing a simple and generic thread pool in C ++ 11

I want to create a thread pool for experimental purposes (and for the fun factor). It should be able to process a wide variety of tasks (so I can possibly use it in later projects). In my thread pool class I'm going to need some sort of task queue. S

Why 6-7 threads faster than 20?

In school we were introduced to C++11 threads. The teacher gave us a simple assessment to complete which was to make a basic web crawler using 20 threads. To me threading is pretty new, although I do understand the basics. I would like to mention tha

How can I raise a matrix to a power with multiple threads?

I am trying to raise a matrix to a power with multiple threads, but I am not very good with threads. Also I enter the number of threads from keyboard and that number is in range [1, matrix height], then I do the following: unsigned period = ceil((dou

std :: thread ends before I can detach it

If I create an std::thread that terminates before I am able to call detatch() on it, what is the expected behavior? Should an exception be thrown due to the fact that joinable is already false? If this is so, then is there a way to create a thread wh

Compiler error related to copy constructor

I have a resource that is shared between two concurrent threads. The resource contains a vector that both threads need to read and write to. Hence, I make access to the vector exclusive through a mutex. So far so good, sharing of resource works well

How to write lambda function with arguments? c ++

I want to call a method (for this example std::thread constructor) with lambda function, passing int value: int a=10; std::thread _testThread = thread([a](int _a){ //do stuff using a or _a ? }); _testThread.detach(); I don't know how to properly writ

Qt - send a signal from a c ++ thread

I want to emit a signal from a C++ thread (std::thread) in Qt. How can I do it?You definitely can emit a signal from a thread (QThread, std::thread or even boost::thread). Only you must be careful of your connect function's fifth parameter (Qt::Conne

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) empty

C ++ 11 std :: strange behavior thread

I am experimenting a bit with std::thread and C++11, and I am encountering strange behaviour. Please have a look at the following code: #include <cstdlib> #include <thread> #include <vector> #include <iostream> void thread_sum_up(c

Portable method of setting priority std :: thread in C ++ 11

What is the correct way in the post C++11 world for setting the priority of an instance of std::thread Is there a portable way of doing this that works at least in Windows and POSIX (Linux) environments? Or is it a matter of getting a handle and usin

Is it possible to set a std :: thread and initialize it later?

My aim is to keep an std::thread object as data member, and initialize it when needed. I'm not able to do this (as in my code below) because the copy constructor of the std::thread class is deleted. Is there any other way to do it? class MyClass { pu

2 wires slower than 1?

I was playing around with std::thread and something weird popped up: #include <thread> int k = 0; int main() { std::thread t1([]() { while (k < 1000000000) { k = k + 1; }}); std::thread t2([]() { while (k < 1000000000) { k = k + 1; }}); t1.joi

Execute a member function of a class

I am trying to experiment the C++11 threads in a way that it accepts a member function of a class as a parameter into the thread constructor as shown in the first code snippet below on line 20 that is marked . The class definition is given in the 2nd

Massive CPU load using std :: lock (c ++ 11)

My recent efforts to implement a thread/ mutex manager ended up in an 75% CPU load (4 core), while all four running threads were either in sleep or waiting for a mutex beeing unlocked. The specific class is far too large for being posted here entirel