Giter Site home page Giter Site logo

gmh5225 / call_once-magic-multi-threading Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sheikhazad/call_once-magic-multi-threading

0.0 1.0 0.0 116 KB

This project shows how std::call_once magically restrict only one thread to call a block of code and avoid wrong use of mutex to protect data, thus increase efficiency.

C++ 100.00%

call_once-magic-multi-threading's Introduction

Call_Once-Magic-Multi-Threading

This project shows how std::call_once magically restrict only one thread to call a block of code and avoid wrong use of mutex to protect data, thus increase efficiency. Once a thread calls call_once, then rest of the thread will not call it.

USE OF CALL ONCE INSTEAD OF MUTEX:

  1. No need to lock before checking if file is open.
  2. Call_once will be called by ONLY one thread and so file will be opened by ONLY one thread.
  3. It will avoid multiple threads waiting on mutex, lock and unlock unnecessarily, thus avoid waste of CPU cycles and avoid hindering the program from being run concurrently.
  4. Compare it with use of mutex in in MutexWrongUsage.cpp

WRONG USE OF MUTEX INSTEAD OF CALL_ONCE:

  1. All threads will wait on this lock and check if file is opened even though file is already opened by one of the thread
  2. It will cause multiple threads waiting on mutex, lock and unlock unnecessarily, it wastes CPU cycles and hinders the program from being run concurrently.
  3. Wrong use of mutex can be resolved by use of once_flag, which is shown in CallOnceUsage.cpp

call_once-magic-multi-threading's People

Contributors

sheikhazad avatar

Watchers

 avatar

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.