ACE notes (7) -ACE monomer instance, primitive, lock, condition variable

xiaoxiao2021-03-05  28

ACE notes (7) -ACE monomer instance, primitive, lock, condition variable

First introduce the concept of the ACE monomer instance so that the monomer instance is a static global object, its lifecycle is managed by the ACE mechanism, so it is absolutely not to call the system underlayer in the main function, and exit the main function. Otherwise, these monomer instances will not be released, default, ACE generates some common or special monomer instances when the program starts.

You can also generate your own monomer class by template technology, as follows: #include "ace / auto_event.h" #include "ace / singleton.h" #include "ace / thread_manager.h"

TYPEDEF ACE_SIINGLETON Event; the above code defines a single_auto_event type single class, its lock mechanism is an instance of the thread mutex to obtain a monomer class, need to call instance () methods, such as: Event :: Instance () -> WAIT ()

Of course, this is understood to have the compiler templates need to add the following code to help understand the compiler: #if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION) template class ACE_Singleton ; #elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA) #pragma instantiate ACE_Singleton #elif defined (__GNUC__) && (defined (_AIX) || defined (__hpux)) template ACE_Singleton * ACE_Singleton :: singleton_; #endif / * ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION * / ACE primitive: can be used The following code defines a working operation being an atomic action, such as ACE_ATOMIC_OP count; the above code defines the variable count, the lock mechanism uses the thread mutex mechanism, so after the COUNT variable, such as , - The operation does not have to consider multi-threaded problems.

Lock and conditional variables: Lock ensures that resources are only one thread operating condition variables in a certain time, which is usually required to use the common class: ACE_THREAD_MUTEX class: acquire: get the right to lock, if Specified time is not available, return -1 Release method: Release lock Use rights acquire_read () get the lock read power ACQUIRE_WRITE () get the write rights ACE_THREAD_MUTEX class ACE_CONDITION_THREAD_MUTEX class ACE_CONDITION_THREAD_MUTEX (Lock): Construct a lock condition variable wait () Waiting condition Established Signal () Send condition to set up a signal

转载请注明原文地址:https://www.9cbs.com/read-33904.html

New Post(0)