8000 GitHub - jbcoe/mutex_protected: A class template to manage mutex protection of a resource
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

jbcoe/mutex_protected

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mutex_protected: A Mutex that Owns the Resource it Protects

codecov language license issues pre-commit

This repository contains the class mutex_protected, which is a mutex that owns the value it protects, and uses the type system and RAII to enforce that only one thread can access it at any given time.

It is clearly possible to do the same thing with std::mutex and std::lock_guard, but those are easy to misuse, accessing a variable without locking the mutex.

The goal of this project is to push an implementation of mutex_protected to the C++ Standard Library. Similar implementations exist in Boost and Folly.

Use

The mutex_protected class template is header-only. To use it, include the header mutex_protected.h in your project.

#include <thread>
#include <vector>

#include "mutex_protected.h"

int main() {
  mutex_protected<int> value(0);

  std::vector<std::thread> threads;
  threads.reserve(10);
  for (int i = 0; i < 10; ++i) {
    threads.emplace_back([&value]() {
      for (int j = 0; j < 10000; ++j) {
        *value.lock() += 1;
      }
    });
  }
  for (auto& thread : threads) {
    thread.join();
  }
  return *value.lock() == 100000;
}

License

This code is licensed under the MIT License. See LICENSE for details.

Developer Guide

For building and working with the project, please see the developer guide.

GitHub codespaces

Press . or visit [https://github.dev/jbcoe/mutex_protected] to open the project in an instant, cloud-based, development environment. We have defined a devcontainer that will automatically install the dependencies required to build and test the project.

About

A class template to manage mutex protection of a resource

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  
0