From 6b3899c1410d991c5b0b10f6011a6e09d22a6f01 Mon Sep 17 00:00:00 2001 From: Steve Clement Date: Tue, 16 Jul 2013 12:59:43 +0200 Subject: [PATCH] - More information on libs used - amended Makefile to make the stock glibmm thread test compile - amended Makefile in assumption that compile will fail too when source files are given at wrong position - add thread.cc from the glibmm example directory (good test to see all is fine) - fixed test.cpp (but doesn't compile yet) --- HACKING | 3 ++ frameserver/Makefile | 6 ++- frameserver/test.cpp | 4 +- frameserver/thread.cc | 113 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 122 insertions(+), 4 deletions(-) create mode 100644 frameserver/thread.cc diff --git a/HACKING b/HACKING index beb96fa..30c50ed 100644 --- a/HACKING +++ b/HACKING @@ -6,6 +6,9 @@ Currently used versions of dependencies: -- Documentation: http://www.boost.org/doc/libs/ - libglibmm 2.35.9 -- Documentation: https://developer.gnome.org/glibmm/2.35/ +- libglib 2.36.0 +-- Documentation: https://developer.gnome.org/glib/2.36/ +-- Deprecation notices: https://developer.gnome.org/glib/2.36/glib-Deprecated-Thread-APIs.html - libncurses 5.9 -- Documentation: http://www.tldp.org/HOWTO/NCURSES-Programming-HOWTO/ diff --git a/frameserver/Makefile b/frameserver/Makefile index 4144e55..38282dd 100644 --- a/frameserver/Makefile +++ b/frameserver/Makefile @@ -1,4 +1,6 @@ frameserver: - g++ -O2 -ggdb -lncurses `pkg-config --libs --cflags glibmm-2.4` `pkg-config --libs --cflags gthread-2.0` -lboost_system -o frameserver Buffer.cpp Buffers.cpp Server.cpp frameserver.cpp + g++ -O2 -ggdb -lncurses Buffer.cpp Buffers.cpp Server.cpp frameserver.cpp `pkg-config --libs --cflags glibmm-2.4` `pkg-config --libs --cflags gthread-2.0` -lboost_system -o frameserver test: - g++ -O2 -pipe -fomit-frame-pointer `pkg-config --libs --cflags glibmm-2.4` `pkg-config --libs --cflags gthread-2.0` -o test Buffer.cpp Buffers.cpp test.cpp + g++ -O2 -pipe -fomit-frame-pointer Buffer.cpp Buffers.cpp test.cpp `pkg-config --libs --cflags glibmm-2.4` `pkg-config --libs --cflags gthread-2.0` -o test +thread_test: + g++ -O2 -pipe -fomit-frame-pointer thread.cc -o thread `pkg-config --cflags --libs glibmm-2.4` diff --git a/frameserver/test.cpp b/frameserver/test.cpp index 0fe490e..23f851f 100644 --- a/frameserver/test.cpp +++ b/frameserver/test.cpp @@ -31,8 +31,8 @@ int main(void) buffers = new Buffers(NUMBUFS); - vector readers; - vector writers; + vector readers; + vector writers; for(int i = 0; i < NUMTHREADS; i++) { diff --git a/frameserver/thread.cc b/frameserver/thread.cc new file mode 100644 index 0000000..6cfa904 --- /dev/null +++ b/frameserver/thread.cc @@ -0,0 +1,113 @@ + +#include +#include +#include +#include +#include +#include + +namespace +{ + +class MessageQueue : public sigc::trackable +{ +public: + MessageQueue(); + ~MessageQueue(); + + void producer(); + void consumer(); + +private: + Glib::Threads::Mutex mutex_; + Glib::Threads::Cond cond_push_; + Glib::Threads::Cond cond_pop_; + std::queue queue_; +}; + + +MessageQueue::MessageQueue() +{} + +MessageQueue::~MessageQueue() +{} + +void MessageQueue::producer() +{ + Glib::Rand rand (1234); + + for(int i = 0; i < 200; ++i) + { + { + Glib::Threads::Mutex::Lock lock (mutex_); + + while(queue_.size() >= 64) + cond_pop_.wait(mutex_); + + queue_.push(i); + std::cout << '*'; + std::cout.flush(); + + cond_push_.signal(); + } + + if(rand.get_bool()) + continue; + + Glib::usleep(rand.get_int_range(0, 100000)); + } +} + +void MessageQueue::consumer() +{ + Glib::Rand rand (4567); + + for(;;) + { + { + Glib::Threads::Mutex::Lock lock (mutex_); + + while(queue_.empty()) + cond_push_.wait(mutex_); + + const int i = queue_.front(); + queue_.pop(); + std::cout << "\x08 \x08"; + std::cout.flush(); + + cond_pop_.signal(); + + if(i >= 199) + break; + } + + if(rand.get_bool()) + continue; + + Glib::usleep(rand.get_int_range(10000, 200000)); + } +} + +} + + +int main(int, char**) +{ + Glib::init(); + + MessageQueue queue; + + Glib::Threads::Thread *const producer = Glib::Threads::Thread::create( + sigc::mem_fun(queue, &MessageQueue::producer)); + + Glib::Threads::Thread *const consumer = Glib::Threads::Thread::create( + sigc::mem_fun(queue, &MessageQueue::consumer)); + + producer->join(); + consumer->join(); + + std::cout << std::endl; + + return 0; +} +