Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Synchro#1
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
// Реализовать доступ к данным класса Storage в режиме "один писатель, много читателей", гарантирующий отсутствие // взаимных блокировок, коллизий и модификаций данных читающим тредом при вставках/обращениях. // (U1 зависит от U0 и находится с последним в отношении N:1). #include <iostream> #include <vector> #include <map> #include <boost/thread.hpp> typedef boost::shared_lock<boost::shared_mutex> read_lock; typedef boost::unique_lock<boost::shared_mutex> write_lock; struct U0 { boost::shared_mutex mylock; // ... // some data }; struct U1 { boost::shared_mutex mylock; boost::weak_ptr<U0> u0; // ... // some data }; class Storage { boost::shared_mutex mylock; std::map<int, boost::shared_ptr<U0>> u0s; std::map<int, boost::shared_ptr<U1>> u1s; public: Storage(){ /*auto& u0 = u0s[0]; auto& u1 = u1s[0]; u0.reset(new U0); u1.reset(new U1); u1->u0 = u0;*/ } enum class u0index{}; enum class u1index{}; // Доступ на чтение/запись к Storage // ... put<lock_type>(); // Блокируется на чтение/запись U0 // ... get<lock_type>(u0index idx); // Блокируется на чтение/запись пара (U1, U0) // ... get<lock_type>(u1index idx); // Вставка U0 // ... put(const U0&); // Вставка (U1[, U0]) // ... put(const U0&, const U1&); }; int main() { /*Storage storage; U0& u0 = storage.get<read_lock>((Storage::u0index)0);*/ return 0; }
g++
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
Compilation time: 4.56 sec, absolute running time: 0.24 sec, cpu time: 0.18 sec, memory peak: 3 Mb, absolute service time: 4,82 sec
edit mode
|
history
|
discussion