"Fossies" - the Fresh Open Source Software Archive

Member "tcpflow-1.6.1/src/be13_api/cppmutex.h" (19 Feb 2021, 1343 Bytes) of package /linux/misc/tcpflow-1.6.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "cppmutex.h" see the Fossies "Dox" file reference documentation.

    1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
    2 
    3 /**
    4  * Cppmutex is an easy-to-use mutex class.
    5  * Create a cppmutex instance for a mutex.
    6  * Create a cppmutex::lock(M) object to get a lock; delete the object to free it.
    7  *
    8  * BE SURE THAT HAVE_PTHREAD IS DEFINED BEFORE INCLUDING THIS FILE
    9  */
   10 
   11 
   12 #ifndef CPPMUTEX_H
   13 #define CPPMUTEX_H
   14 
   15 #include <stdlib.h>
   16 #include <iostream>
   17 #include <errno.h>
   18 #include <string.h>
   19 
   20 #include <pthread.h>
   21 #include <exception>
   22 
   23 class cppmutex {
   24     // default copy construction and assignment are meaningless and not implemented
   25     cppmutex(const cppmutex &c);
   26     cppmutex &operator=(const cppmutex &cp);
   27 
   28 public:
   29     pthread_mutex_t M;
   30 public:
   31     cppmutex():M(){
   32         if(pthread_mutex_init(&M,NULL)){
   33             std::cerr << "pthread_mutex_init failed: " << strerror(errno) << "\n";
   34             exit(1);
   35         }
   36     }
   37     virtual ~cppmutex(){
   38         pthread_mutex_destroy(&M);
   39     }
   40     class lock {                        // get
   41     private:
   42         cppmutex &myMutex;      
   43         lock(const lock &l);            // copy of locks is meaningless
   44         lock &operator=(const lock &l);
   45     public:
   46         lock(cppmutex &m):myMutex(m){
   47             pthread_mutex_lock(&myMutex.M);
   48         }
   49         ~lock(){
   50             pthread_mutex_unlock(&myMutex.M);
   51         }
   52     };
   53 };
   54 
   55 #endif