Pass a reference into the function
Suppose you have a function that expects a reference argument:
When you instantiate a thread object with this function, and pass in the argument, unfortunately, the supplied value will be copieds:
std::thread mythread(func, some_obj);
To pass a reference into the function, wrap the object up with std::ref():
std::thread mythread(func, std::ref(some_obj));
Multi-thread a member function
This is what you need to do: pass in a member function pointer as the function, and use a pointer/reference to an object as the first argument. If the member function itself expects some arguments, pass them in after the object pointer:
void DoSomething(int i);
std::thread mythread(&MyClass::DoSomething, &myobj, 123);
Move an object in
There are also situations that the objects that you need to pass into the function are not copyable, but only movable, e.g. it's an instance of unique_ptr. So use std::move():
std::thread mythread(func, std::move(your_noncopyable_obj));
std::thread objects is another example of movable objects. You can't copy them, but you can move them, which gives the ownership of a running thread to another std::thread object:
std::thread mysecond = std::move(myfirst);
mysecond = std::move(mythird); //this will terminate the program
The last statement will terminate the whole program. mysecond is holding one running thread when the programm tries to move another one's ownership to it. You can't do this. You have to wait for a thread to finish, or detach it before its destruction. Following this thought, you can's "drop" a thread by moving another thread in. This triggers std::terminate().
std::thread::hardware_concurrency() returns the number of threads that can truly concurrently run. But this value may not reflect the real value. It may give you 0 when such information isn't available. And even if it is available, you may not want to rely on it. Consider it as a hint.
A smart trick from Program Listing 2.8
I'm too lazy to repeat the code here. The problem is pretty simple actually. It's a parallel version of std::accumulate().
Now suppose you have N number and you decide to divede them into M groups and let M threads to take care the accumulating of each group. Now instead of give each of them N/M numbers, which is wrong, if such division is uneven, you should give the first M-1 groups to M-1 threads, and give whatever left to the last thread.
A very simple trick, but reduces lots of troubles of dealing with uneven division.
std::thread class has a get_id() member function to provide a thread identifier. Want to know the current thread's id? Use std::this_thread::get_id() function. Pay attention that this_thread is just a namespace, not a class.
Always refer to the header file for details.
I almost forget to mention the book: C++ Concurrency in Action. I'm still in chapter 2, but I strongly recommend this to anyone who's interested in writing concurrent C++11 code. Great book.