Introduction to Network Programming 05

In recent days, because of the working requirements, I have learned multi-threading programming and written a small tool with thread pool. For the callback of network library, multi-threading understanding has deepened a layer of understanding. It is also important to understand these small components by understanding multithreading. Interrupted by the work, it is difficult to read the net module directly. I intend to start with the base module.
class noncopyable {}// deletes assignment constructs and assignment operations directly, and default constructs and destructions use default operations.
class copyable {}// Default the construction and destruct settings, the rest unchanged, compiler settings

//Atomic operations provided by the compiler itself
type __sync_fetch_and_add (type *ptr, type value, ...)
type __sync_fetch_and_sub (type *ptr, type value, ...)
type __sync_fetch_and_or (type *ptr, type value, ...)
type __sync_fetch_and_and (type *ptr, type value, ...)
type __sync_fetch_and_xor (type *ptr, type value, ...)
type __sync_fetch_and_nand (type *ptr, type value, ...)


type __sync_add_and_fetch (type *ptr, type value, ...)
type __sync_sub_and_fetch (type *ptr, type value, ...)
type __sync_or_and_fetch (type *ptr, type value, ...)
type __sync_and_and_fetch (type *ptr, type value, ...)
type __sync_xor_and_fetch (type *ptr, type value, ...)
type __sync_nand_and_fetch (type *ptr, type value, ...)
//The difference between the two sets of functions is that the first group returns the value before the update and the second group returns the updated value.

bool __sync_bool_compare_and_swap (type *ptr, type oldval type newval, ...)
type __sync_val_compare_and_swap (type *ptr, type oldval type newval, ...)
//These two functions provide comparison and exchange of atoms. If * ptr == oldval, newval is written to * ptr.
//The first function returns true when it is equal and written.
//The second function returns the value before the operation.

type __sync_lock_test_and_set (type *ptr, type value, ...)
   //Set * ptr to value and return the value before the * ptr operation.

void __sync_lock_release (type *ptr, ...)
     //Set * ptr to 0
AtomicIntegerT The implementation of atomic classes takes advantage of gcc Provides atomic operations in c++11 Yes, there are. std::atomic<int> class
template <typename T>
class AtomicIntegerT:noncopyable{
public:
    T get() {
        return __sync_val_compare_and_swap(&value_, 0, 0);
    }
    T getAndAdd(T x) {
       return __sync_fetch_and_add(&value_, x);
    }
    T addAndGet(T x) {
       return getAndAdd(x) + x;
    }
    T incrementAndGet() {
       return addAndGet(1);
    }
    T decrementAndGet() {
       return addAndGet(-1);
    }
     void add(T x) {
       getAndAdd(x);
     }
     void increment() {
       getAndAdd(1);
     }
     void decrement() {
             getAndAdd(-1);
     }
     T getAndSet(T x) {
          return __sync_lock_test_and_set(&value_, x);
     }
private:
   volatile T value_;  Initialization is 0
}
typedef detail::AtomicIntegerT<int32_t> AtomicInt32;
typedef detail::AtomicIntegerT<int64_t> AtomicInt64;

Tags: PHP Programming network REST

Posted on Wed, 09 Oct 2019 16:38:47 -0700 by tefflox