Source: http://blog.163.com/angtylook@126/blog/static/8388724820111093470947/
The reason for this is that when learning the thread, the latter part cannot be viewed, and the notes are written badly. Here is the address of the boost documentation Chinese project. If you are interested, you can download the documentation and hope that more talented friends will be added.
Boost document Chinese Project address: http://code.google.com/p/boost-doc-zh/
Boost. Thread
I have heard of multithreading a lot, but I have never really touched it. At most, when reading "Advanced Programming in UNIX environments", I have tried the pthread library example in the book. This is fine in Linux. Although the pthread library can be used in mingw in Windows, it is not very nice, but I don't know how to use Windows Win32 threads.
How boost is powerful, and how it learns from its thread library. It's just that we haven't compiled boost successfully before. Recently, our character burst and it turned out to be successful! So I will understand the boost. Thread library.
The book says you can create a thread like this:
Boost: thread t (thread_function );
Thread_function is a function that is executed immediately in the thread after being created.
Join and detach
Boost: The two methods join () and detach () in the thread. For example, T. Join () will let the calling thread wait until the thread t finishes executing. However, calling T. Detach () will separate t from the corresponding thread. At this time, t no longer represents a thread, so t cannot be used to control the detached thread. I'm thinking this is not to say that the separated thread will not be controlled.
Interrupt
The thread can be interrupted, which is too normal. To interrupt the thread, call T. Interrupt (). But it is not always effective, because the document says that the thread is only in boost. the breakpoint defined by thread checks whether interrupt () is called. If it is called, it is interrupted and a boost: thread_interrupt exception is thrown. Oh, by the way, the central breakpoint is a predefined function, such as sleep. If you call these functions in a thread, it is a central breakpoint.
Boost: this_thread
The boost: this_thread name provides some independent functions for the current thread. For example, sleep () and get_id (). For more information, see the function name. For more information, see the document. I really ran to read the document. Here are a few interesting functions.
Interruption_point ()
Test whether the current thread is interrupted. An exception is thrown.
Bool interruption_requested ()
Whether to request to interrupt the current thread.
Thread Synchronization
The use of threads certainly cannot avoid the synchronization of shared resources. I want to get dizzy with what semaphores, processes, and so on when I was learning the operating system. To be honest, I am also dizzy when I think of these. When I see in the book that boost provides multiple mutex classes, it is really a fear, but for now I will only learn the simplest, complicated to use.
The simplest is like this:
Boost: mutex;
Mutex. Lock ();
Critical section;
Mutex. Unlock ();
In addition to the above form, you can also do this:
Boost: lock_guard <boost: mutex> lock (mutex)
Critical section;
In the boost: lock_guard internal constructor and destructor, lock () and unlock () are called (). So when critical
When the section ends, it just leaves the scope. It is very convenient and appropriate to use lock_guard.
Boost: timed_mutex this only supports timed_lock (). It is used to block a period of time and will be returned no matter whether the lock can be obtained after the time. Boost: shared_mutex literal meaning, shared mutex volume, maybe the shared lock is easier to understand, it is not clear whether it can be understood like this. Like lock_guard, there are two similar boost: unique_lock and boost: shared_lock, which are used together with boost: shared_mutex.
Synchronization and threads store a lot of content locally, and you can't keep writing it down. This is the end of the day. I understood it and felt like I could continue.