Skip to content

VTroyanGolovyan/ConcurrentAlgorithmsAndDS

Repository files navigation

ConcurrentAlgorithms

Implemented structures:

SMR schema(safe memory reclamation):

Project contains some popular synchronization primitives implementation:

Synchronization:

Other:

Documentation

You can use doxygen to get docs.

doxygen Doxyfile

Usage examples:

Echo Server

synchronize::tp::ThreadPool scheduler{2};
fiber::IoService io(9000);
scheduler.Start();

fiber::Go(scheduler, [&] {
    while (true) {
        auto socket_ptr = io.Accept(); // fiber will be suspended, while asynchronous accept works
        fiber::Go(scheduler, [&wg, &io, socket_ptr]() mutable {
          while (true) {
              auto res = io.ReadSome(*socket_ptr); // fiber will be suspended, while asynchronous read works
              io.Write(*socket_ptr, res); // fiber will be suspended, while asynchronous write works
          }
        });
    }
});

io.RunService();
scheduler.Stop();

Fibers

synchronize::tp::ThreadPool scheduler{2};
scheduler.Start();

synchronize::WaitGroup wg;

std::atomic<int> x{0};
for (size_t i = 0; i < 400000; ++i) {
    wg.Add(1);
    fiber::Go(scheduler, [&wg, &scheduler, &x] {
        utils::Defer def([&wg]() { wg.Done(); });
        for (size_t j = 0; j < 100; ++j) {
            ++x;
            fiber::Yield();
        }
    });
}

wg.Wait();
std::cout << x.load();
scheduler.Stop();

ThreadPool

synchronize::tp::ThreadPool tp(4);
std::atomic<int> cnt = 0;
tp.Start();
synchronize::WaitGroup wg;

for (size_t i = 0; i < 1000; ++i) {
    wg.Add(1);
    tp.Submit(
        [&, k=i]() {
            utils::Defer def([&wg]() { wg.Done(); });
            for (size_t i = 0; i < 10000; ++i) {
                ++cnt;
            }
        }
    );
}

wg.Wait();
std::cout << cnt << std::endl;
tp.Stop();
std::cout << cnt << std::endl;

Third-party:

About

Some popular concurrent algorithms and data structures

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published