I am reading documents about c++11 multi-threads, and met this example for std::thread.
Code:
void thread_task(int n) {
...
}
int main(int argc, const char *argv[])
{
std::thread threads[5];
for (int i = 0; i < 5; i++) {
threads[i] = std::thread(thread_task, i + 1);
}
return 0;
}
I do not understand threads[i] = std::thread(thread_task, i + 1);. Is the std::thread a static function call, and returns a reference for std::thread object? Sounds inconceivable, but seems to be what the code say.
Because I would write it like this:
std::thread *threads[5];
for (int i = 0; i < 5; i++) {
threads[i] = new std::thread(thread_task, i + 1);
}
Thanks.
Let's go through exactly what is happening:
std::thread threads[5];
This creates an array of 5 std::thread objects, which are default constructed. Currently they represent "not a thread" as this is the state default construction leaves them in.
for (int i = 0; i < 5; i++) {
threads[i] = std::thread(thread_task, i + 1);
}
This uses the move form of operator=. Because threads are not copyable, only moveable, thread& operator=(thread&& t) is defined while thread& operator=(const thread& t) is not. This assigns the thread object in threads[i] to the newly constructed std::thread(thread_task, i + 1);.
There is no reason to use an array of pointers here. It adds nothing but the possibility for memory leaks.
Is the std::thread a static function call, and returns a reference for std::thread object?
No, it's creating a temporary thread object; just as int(42) creates a temporary integer. The assignment moves that into the array, since threads are movable and temporaries can be moved from.
Because I would write it like this:
You're introducing dynamic allocation, an extra level of indirection, and memory leaks (unless you also add code to delete them when you're finished) for no good reason.
The default constructor of std::thread creates an instance that does not represent a thread. The assignment is a move-assignment, since copies are also not allowed. It moves the real instance created by std::thread(thread_task, i + 1); into the array.
This:
std::thread(thread_task, i + 1)
is a call to the std::thread constructor, which creates a new thread object and passes it a pointer to the thread_task function, which itself is called with i+1 as parameter.
The assignment is a move assignment (because the right hand side refers to an anonymous object), not a copy assignment. Note that the std::thread copy constructor and copy assignment operator are deleted.
This is actually a cleaner way than using pointers, because the std::thread objects will be destructed automatically when the threads array goes out of scope.
Related
So, for instance, I have the following code which I want a object's pointer
member to point to a memory which was pointed by another temporary object's
member.
struct A {
int * vals;
A(): vals(new int[10]) { }
~A(){ delete[] vals; }
};
int main() {
A a;
{
A temp;
for (int i = 0; i < 10; ++i) {
temp.vals[i] = 100;
}
a.vals = temp.vals;
temp.vals = nullptr; // avoid double free
}
I set temp.vals to nullptr in case the destructor of temp will free that
memory. So far so good, I guess. However, if I change the vals to a dynamic
array, i.e. a pointer to pointers:
struct A {
int ** vals;
A(): vals(new int*[10]) {
for (int i = 0; i < 10; ++i) {
vals[i] = new int;
}
}
~A(){
for (int i = 0; i < 10; ++i) {
delete vals[i]; // illegal to dereference nullptr
}
delete [] vals;
}
};
int main() {
A a;
{
A temp;
for (int i = 0; i < 10; ++i) {
temp.vals[i] = new int(1);
}
a.vals = temp.vals;
temp.vals = nullptr; // avoid double free
}
}
I have add a for loop in destructor to handle the nested allocated memory, and
to avoid the memory be freed by the destructor of temp, I set temp.vals to
nullptr, which, however will cause a segmentation fault since when destructor
of temp is called, it is illegal to dereference a nullptr.
So my question is, how to correct set the destructor to handle the dynamic array.
I'm not a native speaker, so please forgive my grammar mistakes.
The typical C++ solution looks a bit different:
class A {
private:
int* vals;
public:
A(): vals(new int[10]) { }
~A(){ delete[] vals; }
A (A const& src); // Copy constructor
A (A&& src) : vals (src.vals) { src.vals = nullptr; }
A& operator=(A const&); // Assignment
A& operator=(A &&);
};
You can now write a = std::move(temp). Outside the class, you don't need to know how the inside works.
For your 2D array, just define the same special member functions. This is usually called the "Rule of Five". If you need a destructor, you probably need the other 4 functions as well. The alternative is the "Rule of Zero". Use std::vector or another class that manages memory for you.
However, if I change the vals to a dynamic array, i.e. a pointer to pointers
In the first program, vals is a pointer. It points to a dynamic array of integers. In the second program, vals is also a pointer. It points to a dynamic array of pointers.
how to correct set the destructor to handle the dynamic array.
You set vals to null. If null is a valid state for vals, then it isn't correct to unconditionally indirect through it in the destructor. You can use a conditional statement to do so only when vals isn't null.
However, the program is hardly safe because vals is public, and thus it is easy to mistakenly write a program where it is assigned to point to memory that isn't owned by the object. In cases where destructor cleans up an owned resource, it is important to encapsulate the resource using private access to prevent accidental violation of class invariants that are necessary to correctly handle the resource.
Now that vals is no longer outside of member functions, you cannot transfer the ownership like you did in your example. The correct way to transfer the ownership is to use move assignment (or constructor). The implicitly generated move constructor cannot handle an owning bare pointer correctly. You must implement them, as well as the copy assignment and constructor.
Furthermore, you should use an owning bare pointer in the first place. You should use a smart pointer instead. If you simply replaced the bare pointers with unique pointer, then the implicitly generated move assignment and constructor would handle the resource correctly, and the copy assignment and constructor would be implicitly deleted.
Lastly, the standard library has a container for dynamic arrays. Its called std::vector. There's typically no need to attempt to re-implement it. So in conclusion, I recommend following:
std::vector<int> a;
{
std::vector<int> temp;
for (int i = 0; i < 10; ++i) {
temp.vals[i] = 1;
}
a = std::move(temp);
}
There are still issues such as the temporary variable being entirely unnecessary, and the loop could be replaced with a standard algorithm, but I tried to keep it close to the original for the sake of comparison.
P.S. It's pretty much never useful to dynamically allocate individual integers.
My problem deals with the usage of different vectors at the same time. I know I can't expect the same vector to work in multiple threads simultaneously. I've broken down the program so it is easier to understand it. I have a ThreadClass class that has a constructor which just adds an element to the vector k and then calls a thread toCall which then outputs the size of the vector which is supposed to be one. The object of this class is created inside of a different vector inside of the main() function using vector's push_back member.
The output turns out to be 0. Sometimes I can get 1 as well. I can produce more of the number 1 if I switch to debug mode. I have tested this problem on a gnu C++17 compiler (Ubuntu 16.04) and a Visual Studio compiler (Windows 10). My question is now if this example shows that I should avoid using vectors in multi-threaded programs totally?
class ThreadClass
{
private:
std::vector<int> k;
std::thread thr;
public:
ThreadClass() {
k.push_back(27);
thr = std::thread(&ThreadClass::toCall, this);
}
void toCall() {
std::cout << k.size() << std::endl;
}
void close() {
if (thr.joinable())thr.join();
}
};
int main(){
std::vector<ThreadClass> lols;
lols.push_back(ThreadClass());
lols[0].close();
return 0;
}
The problem is that a value of type ThreadClass holds a reference to itself. Specifically, thr contains a copy of this.
When you copy or move such a value, e.g. when the temporary ThreadClass() is moved into lols, the duplicate holds a duplicate this ptr, i.e. it points to the old temporary, whose lifetime ends after the call to lols.push_back finishes.
We can replicate this problem without threads:
class Foo
{
private:
std::vector<int> k;
Foo* possibly_this;
public:
Foo() {
k.push_back(27);
possibly_this = this;
}
void toCall() {
std::cout << possibly_this->k.size() << std::endl;
}
};
int main(){
std::vector<Foo> lols;
lols.push_back(Foo{});
lols[0].toCall();
}
(For me, it prints 0 with -O0 on 7.3.1, but again, it's UB, so it could do anything on your machine.)
lols.emplace() will not help. If a std::vector resizes, then all pointers/iterators into it are invalidated. Unfortunately, you can't change the pointers stored in thr, so you're left with one solution: disable ThreadClass's copy and move constructors, like so:
//within the definition of ThreadClass
ThreadClass(ThreadClass const&) = delete;
In order to place ThreadClass in a container, you will need an additional level of indirection to allow the actual object of a value of type ThreadClass to have a stable location. Either std::list<ThreadClass> or std::vector<std::unique_ptr<ThreadClass>> will do the trick.
One issue is that your thread can call toCall before the constructor returns. It's not a good idea to go creating threads that call back into the object in the constructor. Defer the thread creation to some kind of start or launch function and call that after the constructor returns.
This is also a problem:
lols.push_back(ThreadClass());
Here, the destructor (of the temporary) can even run before toCall gets called! That's definitely not going to work. That's another really good reason not to create a thread in a constructor -- it makes temporary objects disastrous.
This code will explode, right? As soon as the loop exits, the original instances will die with all their inner members so if they weren't PODs, any method like do_stuff which requires access to members of B will throw a segmentation fault, correct?
void foo() {
std::vector<B> bar;
for (int i = 0; i < 7; i++)
bar.push_back(B(i, i, i));
bar[3].do_stuff();
}
So, is there any way to do this without using a pointer?
Or do you have to do this:
void foo() {
std::vector<B*> bar;
for (int i = 0; i < 7; i++)
bar.push_back(new B(i, i, i));
bar[3]->do_stuff();
for (int i = 0; i < 7; i++)
delete bar[i];
}
The first code example is valid.
std::vector will make a copy of the objects you pass them with push_back (or will move them in place with C++11 if you're pushing a temporary) and it will keep all of the instances alive as long as the vector itself is alive.
The destruction will happen when you exit the function, not when you exit the loop.
The first code is better than the second one.
The B instances will be movedsince C++11/copiedpre-C++11 into the vector, so they will not fall out of scope after the loop — only after the vector falls out of scope.
If you want to get the absolutely optimal performance, then do this:
void foo() {
std::vector<B> bar;
bar.reserve(7);
for (int i = 0; i < 7; i++)
bar.emplace_back(i, i, i);
bar[3].do_stuff();
}
This will guarantee only one reallocation, and the elements are constructed directly inside the vector (instead of moving or copying them there) as per Marc Glisse's comments.
No, std::vector takes ownership of its members, so the original code will work fine.
std::vector copies the objects provided to it (by push_back() for example) and keeps them until the the vector itself is destroyed.
So the first code is totally OK as long as the copy constructor of B ( B(const B&) )is implemented properly.
From what I could gather in C++ online documentation, assigning to a joined std::thread object should call its destructor and represents a legitimate operation. Is this the case?
Here some example to show what I mean:
#include <thread>
#include <vector>
using namespace std;
int main()
{
vector<thread> tvec;
for(int = 0; i < 3; ++i)
{
tvec.push_back(thread(foo));
}
for(size_t i = 0; i < 3; ++i)
{
tvec[i].join();
tvec[i] = thread(foo); // is this ok?
}
for(auto& t : tvec)
{
t.join();
}
}
assigning to a joined std::thread object should call its destructor
No it shouldn't! Destructors are only called when objects are destroyed, hence the name.
and represents a legitimate operation
It's fine as long as the thread is not joinable (as is the case in your example). Otherwise, terminate will be called.
If you were to read the standard, rather than dubious online "documentation", you'd find in [thread.thread.assign]
Effects: If joinable(), calls terminate(). Otherwise, assigns the state of x to *this and sets x to a default constructed state.
Calling the assignment operator on a thread checks to see if the thread is joinable() if it is then std::terminate() is called. If it is not then it assigns the state of the thread on the right hand side to the thread being assigned to. It then leaves the thread on the right hand side in a default constructed state.
This is called a move assignment. The actual assignment operator is deleted.
I create a class type Test and constructor with parameter, however, when I want to assign the constructor I create to a new constructor through the function f, the program crashes! Did anybody know why!?
The code:
class Test
{
public:
int number;
int *a;
Test(int n){
a = new int[n];
}
~Test(){
delete []a;
}
};
Test f(Test Ft1)
{
// Do something.
return Ft1;
}
int main()
{
Test t1(3);
t1.number = 5;
Test t2 = f(t1);
return 0;
}
The problem is that you are deleting twice the same array a when t1 and t2 destructors are called:
t1 and t2 have their member variable a pointing to the same memory location. When you do Test t2 = f(t1), a copy of t1 is created and is assigned to t2. You did not define a specific copy constructor, so the compiler defined it implicitly for you. However it simply copies the value of a (and does not do a new allocation as you might expect).
As best practice, I would recommend to add your own:
- copy constructor
- copy assignment
(cf rule of three)
Concerning the variable member a design:
- If you want t1 and t2 to point to the same array, then you can use shared_ptr
- If you want t1 and t2 to have their own array, then it would be simpler to use a vector<int> for a
Edit: in case you need to use a raw pointer, here is a quick example of how you can manage the memory in copy constructor and operator assignment. May I recommend you to read a reference book about it (for instance Effective C++ , chapter 11)? It will explain you the key concepts and the pitfalls.
class Test{
public:
int number;
int *a;
Test(int n){
a = new int[n];
}
~Test(){
delete [] a;
}
Test(const Test& that)
{
int size = sizeof(that.a);
a = new int[size];
memcpy (a, that.a, sizeof(size));
}
Test& operator=(const Test& that)
{
if (this != &that)
{
delete [] a;
int size = sizeof(that.a);
a = new int[size];
memcpy (a, that.a, sizeof(size));
}
return *this;
}
};
Test f(Test Ft1){
//do something
return Ft1;
}
int main(){
Test t1(3);
t1.number = 5;
Test t2 = f(t1);
// Test t3(t1); // calls copy constructor
// t3 = t1; // calls assignment operator
return 0;
}
The cause of your problem is that there is a thing called "binary copy". When special assignment/copy constructors are not defined, this binary copy kicks in. When one of your object gets copied over another, 2 different instances start own the same array because the pointer gets overwritten and the original array from the destination object gets leaked.
Compiler thinks that it is ok to copy contents of one object to another with a simple memcpy() (the picture is slightly more simplified but in essence what I write is correct). This is a constant source of problems, but this is how the language is defined. There is no way to do it any other way today. Tons of code are written and these tons expect exactly this.
First you need to decide what should happen with this array after copying. Should both objects co-own the array of the source object, should this array be duplicated at this point or anything else. Once you decide this, you need to implement this strategy in the assignment/copy constructors.