Created
July 12, 2013 15:10
-
-
Save tkil/5985200 to your computer and use it in GitHub Desktop.
threaded vector swapping...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Compile line for native build: | |
PATH=/usr/local/gcc/bin:$PATH \ | |
g++ -O3 -std=c++11 -o vector-swap-demo vector-swap-demo.cpp \ | |
-I/usr/local/boost/include \ | |
-pthread -lboost_thread-mt -lpthread -lrt | |
Compile line for cross build: | |
/opt/cross//xtools/bin/powerpc-e300c3-linux-gnu-g++ \ | |
-isystem /opt/cross/stage/include -isystem /opt/cross/stage/usr/include \ | |
-std=c++0x -Woverloaded-virtual -Wold-style-cast -pthread -rdynamic \ | |
-D_FILE_OFFSET_BITS=64 -funsigned-char -I .. -Wconversion -Wall \ | |
-Wextra -Werror -pedantic -pedantic-errors \ | |
-fdata-sections -ffunction-sections -Wl,--gc-sections \ | |
-g -Wall -O3 --param max-vartrack-size=0 -fPIC \ | |
-o vector-swap-demo-ppc32 vector-swap-demo.cpp \ | |
-L/opt/cross/stage/lib -L/opt/cross/stage/usr/lib \ | |
-Wl,-rpath-link=/opt/cross/stage/lib \ | |
-Wl,-rpath-link=/opt/cross/stage/usr/lib \ | |
-L/opt/cross/build/proj/build/lib \ | |
-pthread -lboost_thread -lboost_system -lpthread -lrt | |
And of course this toy version works. | |
*/ | |
#include <iostream> | |
#include <vector> | |
#include <boost/thread.hpp> | |
class Logger | |
{ | |
public: | |
Logger(); | |
~Logger(); | |
void log( const int i ); | |
private: | |
typedef std::vector< int > IntVec; | |
typedef boost::mutex Mutex; | |
typedef boost::unique_lock< Mutex > Lock; | |
Mutex mutex; | |
boost::condition_variable cond; | |
IntVec queue; | |
bool running; | |
boost::thread printer; | |
void print(); | |
}; | |
Logger::Logger() | |
: running( true ), | |
printer( &Logger::print, this ) | |
{ | |
} | |
Logger::~Logger() | |
{ | |
{ | |
Lock lock( mutex ); | |
running = false; | |
} | |
printer.join(); | |
} | |
void | |
Logger::log( const int i ) | |
{ | |
Lock lock( mutex ); | |
queue.push_back( i ); | |
std::clog << "<q=" << queue.size() << ">" << std::endl; | |
cond.notify_one(); | |
} | |
void | |
Logger::print() | |
{ | |
IntVec work; | |
while ( running ) | |
{ | |
{ | |
Lock lock( mutex ); | |
work.swap( queue ); | |
std::clog << "(w=" << work.size() << ", q=" << queue.size() << ")" << std::endl; | |
if ( work.empty() ) | |
{ | |
cond.timed_wait( lock, boost::posix_time::milliseconds( 500 ) ); | |
continue; | |
} | |
} | |
for ( const int & i : work ) | |
std::cout << i << std::endl; | |
work.clear(); | |
} | |
} | |
int | |
main( int /* argc */, char * /* argv */ [] ) | |
{ | |
Logger l; | |
for ( int i = 0; i < 10; ++i ) | |
{ | |
l.log( i ); | |
} | |
sleep( 1 ); | |
for ( int i = 0; i < 10; ++i ) | |
{ | |
l.log( i ); | |
sleep( 1 ); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment