- adaptepte usleep -> Glib::usleep

- update to glib-2.34 Glib::Threads*

(not tested yet, am on a Mac)
master
Steve Clement 2013-07-15 22:39:25 +02:00
parent dacbfa9b29
commit bccde4f726
5 changed files with 44 additions and 45 deletions

View File

@ -5,45 +5,45 @@ Buffer::Buffer( std::string _id )
id = _id;
}
Buffer::~Buffer()
Buffer::~Buffer()
{
}
void Buffer::set(frame_t data)
{
{
Glib::Mutex::Lock lock(mutex_);
Glib::Threads::Mutex::Lock lock(mutex_);
frame = data;
}
}
void Buffer::set_id(std::string _id)
{
Glib::Mutex::Lock lock(mutex_);
Glib::Threads::Mutex::Lock lock(mutex_);
id = _id;
}
void Buffer::set_selected(bool _selected)
{
Glib::Mutex::Lock lock(mutex_);
Glib::Threads::Mutex::Lock lock(mutex_);
selected = _selected;
}
frame_t Buffer::get()
{
Glib::Mutex::Lock lock(mutex_);
Glib::Threads::Mutex::Lock lock(mutex_);
return frame;
}
std::string Buffer::get_id()
{
Glib::Mutex::Lock lock(mutex_);
Glib::Threads::Mutex::Lock lock(mutex_);
return id;
}
bool Buffer::get_selected()
{
Glib::Mutex::Lock lock(mutex_);
Glib::Threads::Mutex::Lock lock(mutex_);
return selected;
}

View File

@ -16,22 +16,22 @@ class Buffer : public sigc::trackable
public:
Buffer(std::string _id);
~Buffer();
void set(frame_t);
frame_t get();
std::string get_id();
void set_id(std::string id);
bool get_selected();
void set_selected(bool);
private:
std::string id;
frame_t frame;
bool selected;
Glib::Mutex mutex_;
Glib::Threads::Mutex mutex_;
};
#endif

View File

@ -1,6 +1,6 @@
#include "Buffers.h"
Buffers::Buffers()
Buffers::Buffers()
{
id = "";
}
@ -14,26 +14,26 @@ Buffers::Buffers(int _bufnum)
Buffers::~Buffers()
{
Glib::Mutex::Lock lock(mutex_);
Glib::Threads::Mutex::Lock lock(mutex_);
buffers.clear();
}
Buffer* Buffers::get(int index)
{
Glib::Mutex::Lock lock(mutex_);
Glib::Threads::Mutex::Lock lock(mutex_);
return buffers[index];
}
void Buffers::add()
{
Glib::Mutex::Lock lock(mutex_);
Glib::Threads::Mutex::Lock lock(mutex_);
id += "1";
buffers.push_back( new Buffer( id ) );
}
void Buffers::remove(std::string _id)
{
Glib::Mutex::Lock lock(mutex_);
Glib::Threads::Mutex::Lock lock(mutex_);
int size = buffers.size();
for( int i = 0; i < size; i++ )
{

View File

@ -5,7 +5,7 @@
vector of pointers to "Buffer" objects. It automates locking during
all operations and generates "ID" hashes for the buffers upon creation. */
/* TODO: * create hashes during buffer creation
/* TODO: * create hashes during buffer creation
* throw and handle exceptions */
#include <glibmm.h>
@ -24,21 +24,21 @@ class Buffers : public sigc::trackable
public:
Buffers();
Buffers(int);
~Buffers();
void add();
void remove(std::string);
void set_selected_buffer(std::string);
std::string get_selected_buffer();
Buffer* get(int);
private:
vector<Buffer*> buffers;
std::string id;
Glib::Mutex mutex_;
Glib::Threads::Mutex mutex_;
std::string selected_buffer;
};
#endif

View File

@ -9,7 +9,6 @@
#include "Buffer.h"
#include "Buffers.h"
#define NUMBUFS 100
#define NUMTHREADS 20
#define USECS 1000
@ -22,14 +21,14 @@ Buffers* buffers;
void reader(void);
void writer(void);
int main(void)
int main(void)
{
srand( time(NULL) );
Glib::thread_init();
Glib::init();
// our main loop with support for signals and all that jazz
//Glib::RefPtr<Glib::MainLoop> Main = Glib::MainLoop::create();
buffers = new Buffers(NUMBUFS);
vector<Glib::Thread*> readers;
@ -37,12 +36,12 @@ int main(void)
for(int i = 0; i < NUMTHREADS; i++)
{
readers.push_back( Glib::Thread::create( sigc::ptr_fun( &reader), false ) );
writers.push_back( Glib::Thread::create( sigc::ptr_fun( &writer), false ) );
readers.push_back( Glib::Threads::Thread::create( sigc::ptr_fun( &reader), false ) );
writers.push_back( Glib::Threads::Thread::create( sigc::ptr_fun( &writer), false ) );
}
//Main->run();
int count = 0;
while(1) {
count++;
@ -56,28 +55,28 @@ int main(void)
cout << endl;
}
cout << endl;
for(int i = 0; i < SEGWIDTH; i++)
cout << frame.segments[i].r;
cout << endl << endl;
}
usleep( 10000 );
Glib::usleep( 10000 );
}//*/
return 0;
}
void reader(void)
void reader(void)
{
bool quit = false;
frame_t frame;
int bufnum = 0;
while( !quit )
{
{
bufnum = rand()%NUMBUFS;
frame = buffers->get(bufnum)->get();
// cout << "read " << bufnum << endl;
usleep( rand()%USECS );
Glib::usleep( rand()%USECS );
}
}
@ -85,7 +84,7 @@ void writer(void)
{
frame_t frame;
bool quit = false;
int bufnum = 0;
int bufnum = 0;
while( !quit )
{
bufnum = rand()%NUMBUFS;
@ -96,16 +95,16 @@ void writer(void)
frame.windows[i][j] = rand()%255;
}
}
for(int i = 0; i < 12; i++)
{
frame.segments[i].r = 33+rand()%90;
frame.segments[i].g = 33+rand()%90;
frame.segments[i].b = 33+rand()%90;
}
buffers->get(bufnum)->set( frame );
// cout << "wrote " << bufnum << endl;
usleep( rand()%USECS );
Glib::usleep( rand()%USECS );
}
}