Don't allow readers reach the buffer currently being filled.
This commit is contained in:
parent
3a38be042a
commit
d442696cb8
3 changed files with 7 additions and 4 deletions
2
ddcd.cpp
2
ddcd.cpp
|
@ -267,6 +267,8 @@ void* client_thread (void* param) //!TODO
|
||||||
{
|
{
|
||||||
client_t* me_the_client = (client_t*)param;
|
client_t* me_the_client = (client_t*)param;
|
||||||
|
|
||||||
|
|
||||||
|
pthread_exit(NULL);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ tsmpool::tsmpool(size_t size, int num)
|
||||||
this->num = num; //number of buffers of (size) to alloc
|
this->num = num; //number of buffers of (size) to alloc
|
||||||
this->ok = 1;
|
this->ok = 1;
|
||||||
this->lowest_read_index = -1;
|
this->lowest_read_index = -1;
|
||||||
|
this->write_index = 0;
|
||||||
if (pthread_mutex_init(&this->mutex, NULL) != 0) this->ok=0;
|
if (pthread_mutex_init(&this->mutex, NULL) != 0) this->ok=0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,7 +15,7 @@ size_t tsmpool::get_size() { return this->size; }
|
||||||
|
|
||||||
void* tsmpool::get_write_buffer()
|
void* tsmpool::get_write_buffer()
|
||||||
{
|
{
|
||||||
if(write_index==index_before(lowest_read_index)) return NULL;
|
//if(write_index==index_before(lowest_read_index)) return NULL;
|
||||||
void* to_return = buffers[write_index];
|
void* to_return = buffers[write_index];
|
||||||
write_index=index_next(write_index);
|
write_index=index_next(write_index);
|
||||||
}
|
}
|
||||||
|
@ -24,7 +25,7 @@ tsmthread_t* tsmpool::register_thread()
|
||||||
if(!ok) return NULL;
|
if(!ok) return NULL;
|
||||||
pthread_mutex_lock(&this->mutex);
|
pthread_mutex_lock(&this->mutex);
|
||||||
tsmthread_t* thread = new tsmthread_t;
|
tsmthread_t* thread = new tsmthread_t;
|
||||||
thread->read_index = write_index;
|
thread->read_index = index_before(write_index);
|
||||||
threads.push_back(thread);
|
threads.push_back(thread);
|
||||||
pthread_mutex_unlock(&this->mutex);
|
pthread_mutex_unlock(&this->mutex);
|
||||||
return thread;
|
return thread;
|
||||||
|
@ -45,7 +46,7 @@ int tsmpool::remove_thread(tsmthread_t* thread)
|
||||||
|
|
||||||
void* tsmpool::get_read_buffer(tsmthread_t* thread)
|
void* tsmpool::get_read_buffer(tsmthread_t* thread)
|
||||||
{
|
{
|
||||||
if(thread->read_index==write_index) return NULL;
|
if(thread->read_index==index_before(write_index)) return NULL;
|
||||||
void* to_return = buffers[thread->read_index];
|
void* to_return = buffers[thread->read_index];
|
||||||
thread->read_index=index_next(thread->read_index);
|
thread->read_index=index_next(thread->read_index);
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,7 @@ private:
|
||||||
pthread_mutex_t mutex;
|
pthread_mutex_t mutex;
|
||||||
int ok;
|
int ok;
|
||||||
int write_index; //it always points to the next buffer to be written
|
int write_index; //it always points to the next buffer to be written
|
||||||
int lowest_read_index;
|
int lowest_read_index; //unused
|
||||||
|
|
||||||
public:
|
public:
|
||||||
size_t get_size();
|
size_t get_size();
|
||||||
|
|
Loading…
Reference in a new issue