GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - ryanhaining/cppsynchronized: java-like synchronized blocks in c++

BranchesTags
Open more actions menu

Latest commit

 

History

44 Commits
44 Commits

Folders and files

NameName
Last commit message
Last commit date
lockable_objects
lockable_objects
 
 
synctable
synctable
 
 
README.md
README.md
 
 

Repository files navigation

CPP Synchronized

When I first discovered Java's synchronized blocks, I was intrigued. It's pretty easy to forget to unlock a mutex, though RAII and unique_lock aid with this, and easy to follow the structure of a block. I thought for a while about how I might implement something like this in C++11, and eventually accomplished this in two different ways.

synctable

My favorite of the two implementations. Allows for a synchronized block on any pointer using the syntax:

synchronized(&obj) {
    // ... critical section
}

Note that this could have been done using an object rather than a pointer with something more resembling Java synchronized(obj) but it seemed to be more in the spirit of C++ to use the address.

This is accomplished with a #define, and a for loop that initializes an object to lock and unlock a std::mutex on for entry/exit. The mutexes themselves are stored in a global std::unordered_map of void * to mutex.

An alternative use is a tablesynchronized block. This requires the programmer to create a SyncTable and pass that as the first argument to tablesynchronized

SyncTable table;
// ... later on
tablesynchronized(table, &obj) {
    // ... critical section
}

The idea here is a group of threads can use a local SyncTable rather than the global one to avoid contention on the global table.

lockable_objects

A non-intrusive use of multiple inheritance. A "lockable" version of any class can be instantiated with

Lockable<Object> lockable_object(/* Object ctor args */);

Any lockable object can be used within a synchronized block. This approach avoids the SyncTables entirely

synchronized(lockable_object){
    // ... critical section
}

Can then be used without the overhead of a lookup for the mutex. Additionally, no & is used for lockable objects.

About

java-like synchronized blocks in c++

Resources

Readme
Activity

Stars

13 stars

Watchers

2 watching

Forks

3 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.