Code Monkey home page Code Monkey logo

rcumutex's Introduction

RCUMutex

Summary

RCUMutex was created because of the need to have a shared/read lock for a very small critical section. The mutex uses RCU in this implamintaion to allow only one active thread to update a single atomic value at a time, minimizing the time it takes to syncronize cache lines between cores.

RCUMutex works by using by using RCU syncronize to block for exclusive lock while a reader (shared lock) is still active. To allow for exclusize locks to block shared locking the most significate bit of the counter acts as an exclusive flag. When the flag is set accquiring a shared and exclusive lock is block until the flag is cleared.

Usage

Mutexes are not shared between threads for a given resource, instead are maintained by the thread needing access to a particular resource. A mutex can be aquired through the GetMutex method of the RCUManager.

#include<vector>
#include<chrono>
#include<iostream>
#include<functional>

#include<mutex>
#include<shared_mutex>
#include<thread>

#include"RCUMutex.h"

struct SharedResource {
  RCUManager mutexManager;
  int        resourceUnderContention;

  SharedResource(size_t threadCount):mutexManager{threadCount}{}
}

void read(SharedResource &res,unsigned int threadId){
  RCUManager::Mutex mutex=res.mutexManager.GetMutex();

  for(int i=0;i<10;i++){
  
    {
      std::shared_lock<RCUManager::Mutex> lock(mutex);

      std::cout<<"thread "<<threadId<<" resource value is "
               <<res.resourceUnderContention<<" \n";
    }
	
    std::this_thread::sleep_for(std::chrono::milliseconds(10));
  }
}

void write(SharedResource &res){
  RCUManager::Mutex mutex=res.mutexManager.GetMutex();

  for(int i=0;i<10;i++){
  
    {
      std::lock_guard<RCUManager::Mutex> lock(mutex);
	  
      res.resourceUnderContention++;
    }
	
    std::this_thread::sleep_for(std::chrono::milliseconds(10));
  }
}

int main(){  
  const size_t             totalThreads=8;

  SharedResource           res(totalThreads);

  std::vector<std::thread> sharedThreads;

  for(unsigned int i=0;i<totalThreads-1;i++)
    sharedThreads.emplace_back(read,std::ref(res),i);

  std::thread w1(write,std::ref(res));

  for(auto &t:sharedThreads)
    t.join();

  w1.join();

  return 0;
}

rcumutex's People

Contributors

mesonnaise avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

lixin97 xenbo

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.