Coder Social home page Coder Social logo

design-pattern's Introduction

Hi there ๐Ÿ‘‹ Hits

C C++ Go

git-stats

I love programming and problem-solving!

profile

design-pattern's People

Contributors

ryanjeong avatar

Watchers

 avatar  avatar

design-pattern's Issues

Thread-safe Singleton using unique_ptr

// singleton.cc
// g++ singleton.cc -std=c++11 -lboost_system
#include "singleton.hpp"

#include <iostream>

#include <boost/thread/thread.hpp>

std::unique_ptr<Singleton> Singleton::instance = nullptr;
std::once_flag Singleton::flag;

Singleton& Singleton::GetInstance() {
	std::call_once(Singleton::flag, []() {
      Singleton::instance.reset(new Singleton);
  });
	std::cout << "Get Instance." << std::endl;

	return *Singleton::instance;
}

int main() try {
  Singleton::GetInstance();
	Singleton::GetInstance();
	Singleton::GetInstance();

	return 0;
} catch (std::exception& e) {
	std::cout << e.what() << std::endl;
}
// singleton.hpp
#pragma once

#include <memory>  // unique_ptr
#include <mutex>  // once_flag

class Singleton final {
 public:
  ~Singleton() = default;
  Singleton(const Singleton&) = delete;
  Singleton(Singleton&&) = delete;
  Singleton& operator=(const Singleton&) = delete;
  Singleton& operator=(Singleton&&) = delete;
  static Singleton& GetInstance();

 private:
  Singleton() = default;
  static std::unique_ptr<Singleton> instance;
  static std::once_flag flag;
};

Waitfree Single-Producer/Single-Consumer Queue

Waitfree Single-Producer/Single-Consumer Queue

#include <boost/thread/thread.hpp>
#include <boost/lockfree/spsc_queue.hpp>
#include <iostream>

#include <boost/atomic.hpp>

int producer_count = 0;
boost::atomic_int consumer_count (0);

boost::lockfree::spsc_queue<int, boost::lockfree::capacity<1024> > spsc_queue;

const int iterations = 10000000;

void producer(void)
{
    for (int i = 0; i != iterations; ++i) {
        int value = ++producer_count;
        while (!spsc_queue.push(value))
            ;
    }
}

boost::atomic<bool> done (false);

void consumer(void)
{
    int value;
    while (!done) {
        while (spsc_queue.pop(value))
            ++consumer_count;
    }

    while (spsc_queue.pop(value))
        ++consumer_count;
}

int main(int argc, char* argv[])
{
    using namespace std;
    cout << "boost::lockfree::queue is ";
    if (!spsc_queue.is_lock_free())
        cout << "not ";
    cout << "lockfree" << endl;

    boost::thread producer_thread(producer);
    boost::thread consumer_thread(consumer);

    producer_thread.join();
    done = true;
    consumer_thread.join();

    cout << "produced " << producer_count << " objects." << endl;
    cout << "consumed " << consumer_count << " objects." << endl;
}

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.