Skip to content

Commit

Permalink
Synced Chapter05 recipes [2,6]
Browse files Browse the repository at this point in the history
  • Loading branch information
apolukhin committed May 1, 2017
1 parent 3c8ac11 commit ac0db32
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 44 deletions.
1 change: 1 addition & 0 deletions Chapter05/02_mutex/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// In previous recipe we included
// <boost/thread.hpp>, which includes all
// the classes of Boost.Thread.
// Following header includes only boost::thread.
#include <boost/thread/thread.hpp>

namespace without_sync {
Expand Down
8 changes: 2 additions & 6 deletions Chapter05/03_atomics/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,19 @@ boost::atomic<int> shared_i(0);

void do_inc() {
for (std::size_t i = 0; i < 30000; ++i) {
// do some work
// ...
const int i_snapshot = ++ shared_i;

// do some work with i_snapshot
// Do some work with i_snapshot.
// ...
(void) i_snapshot;
}
}

void do_dec() {
for (std::size_t i = 0; i < 30000; ++i) {
// do some work
// ...
const int i_snapshot = -- shared_i;

// do some work with i_snapshot
// Do some work with i_snapshot.
// ...
(void) i_snapshot;
}
Expand Down
30 changes: 16 additions & 14 deletions Chapter05/04_work_queue/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class work_queue {
typedef boost::function<void()> task_type;

private:
std::deque<task_type> tasks_;
boost::mutex tasks_mutex_;
std::deque<task_type> tasks_;
boost::mutex tasks_mutex_;
boost::condition_variable cond_;

public:
Expand Down Expand Up @@ -61,19 +61,19 @@ class work_queue {

work_queue g_queue;

void do_nothing(){}
void some_task();
const std::size_t tests_tasks_count = 3000 /*000*/;

void pusher() {
for (std::size_t i = 0; i < tests_tasks_count; ++i) {
// Adding task to do nothing
g_queue.push_task(&do_nothing);
g_queue.push_task(&some_task);
}
}

void popper_sync() {
for (std::size_t i = 0; i < tests_tasks_count; ++i) {
g_queue.pop_task() // Getting task
(); // Executing task
work_queue::task_type t = g_queue.pop_task();
t(); // Executing task.
}
}

Expand All @@ -86,25 +86,27 @@ int main() {
boost::thread push2(&pusher);
boost::thread push3(&pusher);

// Waiting for all the tasks to push
// Waiting for all the tasks to push.
push1.join();
push2.join();
push3.join();
g_queue.flush();
g_queue.flush();

// Waiting for all the tasks to pop
// Waiting for all the tasks to pop.
pop_sync1.join();
pop_sync2.join();
pop_sync3.join();


// Asserting that no tasks remained,
// and falling though without blocking
// and falling though without blocking.
assert(!g_queue.try_pop_task());

g_queue.push_task(&do_nothing);
g_queue.push_task(&some_task);

// Asserting that there is a task,
// and falling though without blocking
// and falling though without blocking.
assert(g_queue.try_pop_task());

}

void some_task() { /* do nothing */ }
1 change: 1 addition & 0 deletions Chapter05/05_shared_lock/05_shared_lock.pro
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ if (!include(../../config.txt)) {
error("Failed to open config.txt")
}

QMAKE_CXXFLAGS += $$CPP11FLAG
SOURCES += main.cpp
LIBS += -lboost_thread -lboost_system
41 changes: 23 additions & 18 deletions Chapter05/05_shared_lock/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include <map>
#include <unordered_map>
#include <boost/thread/mutex.hpp>
#include <boost/thread/locks.hpp>

namespace first_example {

struct user_info {
std::string address;
unsigned short age;
Expand All @@ -10,61 +12,64 @@ struct user_info {
// ...
};

namespace first_example {

class users_online {
typedef boost::mutex mutex_t;
mutable mutex_t users_mutex_;
std::map<std::string, user_info> users_;
typedef boost::mutex mutex_t;

mutable mutex_t users_mutex_;
std::unordered_map<std::string, user_info> users_;

public:
bool is_online(const std::string& username) const {
boost::lock_guard<mutex_t> lock(users_mutex_);
return users_.find(username) != users_.end();
}

unsigned short get_age(const std::string& username) const {
std::string get_address(const std::string& username) const {
boost::lock_guard<mutex_t> lock(users_mutex_);
return users_.at(username).age;
return users_.at(username).address;
}

void set_online(const std::string& username, const user_info& data) {
void set_online(const std::string& username, user_info&& data) {
boost::lock_guard<mutex_t> lock(users_mutex_);
users_.insert(std::make_pair(username, data));
users_.insert(std::make_pair(username, std::move(data)));
}

// Other methods
// Other methods:
// ...
};

}

using first_example::user_info;

#include <boost/thread/shared_mutex.hpp>

namespace shared_lock_example {


class users_online {
typedef boost::shared_mutex mutex_t;
mutable mutex_t users_mutex_;
std::map<std::string, user_info> users_;
typedef boost::shared_mutex mutex_t;

mutable mutex_t users_mutex_;
std::unordered_map<std::string, user_info> users_;

public:
bool is_online(const std::string& username) const {
boost::shared_lock<mutex_t> lock(users_mutex_);
return users_.find(username) != users_.end();
}

unsigned short get_age(const std::string& username) const {
std::string get_address(const std::string& username) const {
boost::shared_lock<mutex_t> lock(users_mutex_);
return users_.at(username).age;
return users_.at(username).address;
}

void set_online(const std::string& username, const user_info& data) {
boost::lock_guard<mutex_t> lock(users_mutex_);
users_.insert(std::make_pair(username, data));
}

// Other methods
// Other methods:
// ...
};

Expand All @@ -87,7 +92,7 @@ void log_in(T& u, std::size_t count) {
template <class T>
void thread_get_age(T& u) {
for (std::size_t i = 0; i < users_count; ++i) {
u.get_age(boost::lexical_cast<std::string>(i));
u.get_address(boost::lexical_cast<std::string>(i));
}
}

Expand Down
11 changes: 5 additions & 6 deletions Chapter05/06_thread_specific_ptr/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ class connection: boost::noncopyable {
connection(): open_count_(0) {}
};

// In header file
// In header file:
#include <boost/thread/tss.hpp>
connection& get_connection();

// In source file
// In source file:
boost::thread_specific_ptr<connection> connection_ptr;

connection& get_connection() {
Expand All @@ -33,10 +33,10 @@ connection& get_connection() {

void task() {
int result = 2;
// Some computations go there
// Some computations go there.
// ...

// Sending result
// Sending the result:
get_connection().send_result(result);
}

Expand All @@ -61,10 +61,9 @@ int main() {
boost::thread t3(&run_tasks);
boost::thread t4(&run_tasks);

// Waiting for all the tasks to pop
// Waiting for all the tasks to stop.
t1.join();
t2.join();
t3.join();
t4.join();

}

0 comments on commit ac0db32

Please sign in to comment.