caiconghui edited a comment on pull request #8387:
URL: https://github.com/apache/incubator-doris/pull/8387#issuecomment-1061507177


   ```
   #define READ_THREAD_COUNT 8
   #define LOOP_COUNT 5000000
   
   #include <iostream>
   #include <mutex>  // For std::unique_lock
   #include <shared_mutex>
   #include <thread>
   #include <util/mutex.h>
   
   class shared_mutex_counter {
   public:
       shared_mutex_counter() = default;
   
       // Multiple threads/readers can read the counter's value at the same 
time.
       unsigned int get() const {
           std::shared_lock<std::shared_mutex> lock(mutex_);
           return value_;
       }
   
       // Only one thread/writer can increment/write the counter's value.
       void increment() {
           std::unique_lock<std::shared_mutex> lock(mutex_);
           value_++;
       }
   
       // Only one thread/writer can reset/write the counter's value.
       void reset() {
           std::unique_lock<std::shared_mutex> lock(mutex_);
           value_ = 0;
       }
   
   private:
       mutable std::shared_mutex mutex_;
       unsigned int value_ = 0;
   };
   
   class readwrite_mutex_counter {
   public:
       readwrite_mutex_counter() = default;
   
       unsigned int get() const {
           doris::ReadLock rk(&mutex_);
           return value_;
       }
   
       void increment() {
           doris::WriteLock wk(&mutex_);
           value_++;
       }
   
   private:
       mutable doris::RWMutex mutex_;
       unsigned int value_ = 0;
   };
   
   void test_shared_mutex()
   {
       shared_mutex_counter counter;
       int temp;
   
       auto writer = [&counter]() {
           for (int i = 0; i < LOOP_COUNT; i++) {
               counter.increment();
           }
       };
   
       auto reader = [&counter, &temp]() {
           for (int i = 0; i < LOOP_COUNT; i++) {
               temp = counter.get();
           }
       };
   
       std::thread** tarray = new std::thread*[READ_THREAD_COUNT];
   
       clock_t start = clock();
   
       for (int i = 0; i < READ_THREAD_COUNT; i++)
       {
           tarray[i] = new std::thread(reader);
       }
   
       std::thread tw(writer);
   
       for (int i = 0; i < READ_THREAD_COUNT; i++)
       {
           tarray[i]->join();
       }
       tw.join();
   
       clock_t end = clock();
       std::cout << "[test_shared_mutex]" << std::endl;
       std::cout << "thread count:" << READ_THREAD_COUNT << std::endl;
       std::cout << "result:" << counter.get() << " cost:" << end - start << " 
clock temp:" << temp << std::endl;
   }
   
   
   void test_origin_readwrite_mutex()
   {
       readwrite_mutex_counter counter;
   
       int temp;
   
       auto writer = [&counter]() {
           for (int i = 0; i < LOOP_COUNT; i++) {
               counter.increment();
           }
       };
   
       auto reader = [&counter, &temp]() {
           for (int i = 0; i < LOOP_COUNT; i++) {
               temp = counter.get();
           }
       };
   
       std::thread** tarray = new std::thread*[READ_THREAD_COUNT];
   
       clock_t start = clock();
   
       for (int i = 0; i < READ_THREAD_COUNT; i++)
       {
           tarray[i] = new std::thread(reader);
       }
   
       std::thread tw(writer);
   
       for (int i = 0; i < READ_THREAD_COUNT; i++)
       {
           tarray[i]->join();
       }
       tw.join();
   
       clock_t end = clock();
       std::cout << "[test_readwrite_mutex]" << std::endl;
       std::cout << "thread count:" << READ_THREAD_COUNT << std::endl;
       std::cout << "result:" << counter.get() << " cost:" << end - start << " 
clock temp:" << temp << std::endl;
   }
   
   int main() {
       // test_shared_mutex();
       test_origin_readwrite_mutex();
   }
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to