ch2 managing threads

2.2. Passing argument to the std::thread

1. first the implict convertion is execute on which thread.
the thread test demo shows that the implict argument convertion is in the target thread.

2. what is c++ function object? why it could represent a function?
a class with overloaded function call operator.
because it could use itself to call the function.

3.  c/c++ expression parse?

Start reading the declaration from the innermost parentheses, go right, and then go left. When you encounter parentheses, the direction should be reversed. Once everything in the parentheses has been parsed, jump out of it. Continue till the whole declaration has been parsed.

One small change to the right-left rule: When you start reading the declaration for the first time, you have to start from the identifier, and not the innermost parentheses.

https://www.codeproject.com/Articles/7042/How-to-interpret-complex-C-C-declarations

4. the functor
std::thread my_thread(background_task())
background_task() could be parsed as function pointer declaration.

5. the internals
std::thread is just like std::function which could take
1) c function address
2) c++ class member function address and its instance.
3) c++ function object.
etc

6. for non-copyable and movable object
ues std::move

7. want pass by reference
use std::ref()

2.3 transferring ownership of a thread

in one world, std::thread is only movable not copyable.
stl vector is movable aware so could store to std::vector
scope_thread
std::for_each(vec.begin(), vec.end(), std::mem_fn(&std::thread::join))

2.4 chossing the number of threads at runtime

1)std::thread::hardware_concurrency() return the suggest thread.
2)std::accumulate which take a init value and forward iterator end iterator.
2)what is forward iterator/input iterator
Iterator Category  Ability                          Providers
-----------------  -------------------------------  ----------------------------
Input iterator     Reads forward                    istream
Output iterator    Writes forward                   ostream, inserter
Forward iterator   Reads/writes forward             forward_list,
                                                      unordered_[multi]set,
                                                      unordered_[multi]map
Bidirectional it.  Reads/writes forward/backward    list, [multi]set, [multi]map
Random access it.  Reads/writes with random access  vector, deque string, array 

https://stackoverflow.com/questions/5211914/types-of-iterator-output-vs-input-vs-forward-vs-random-access-iterator

2.5 identifying threads
1) completion of comparasion operators
std::thread::id
2) support std::hash template specialization for std::thread::id for std::unorderd_map
which is implemented as hash table.
3) std::thread::id is not integer which should not used to printf %d etc

评论