Re: What's the connection between objects and threads?
"Szabolcs Ferenczi" <szabolcs.ferenczi@gmail.com> wrote in message
news:29cc17bf-e547-48ab-b691-39409fe684b2@d77g2000hsb.googlegroups.com...
On May 21, 10:47 am, James Kanze <james.ka...@gmail.com> wrote:
Besides, you still must show us how can you get elements from
the plain "completely thread safe" STL containers with
multiple consumers. (You cannot show this because you just
talk big as usual.)
Are you trying to say that you cannot use STL containers for
communications between threads?
What I am saying is that you cannot use it without any extra
synchronisation provided there are multiple producer and consumer
threads. That was my original warning what triggered your conditioned
reflex.
<quote>
Be aware that although STL is thread-safe to a certain extent, you
must wrap around the STL data structure to make a kind of a bounded
buffer out of it.
</quote>
http://groups.google.com/group/comp.lang.c++/msg/4450c4f92d6e0211
I use std::deque, for example,
in my message queue, and it works perfectly.
That is your homework to show a solution where two competing threads
are consuming from a "completely thread safe" std::deque.
Yes, you talk big like the Bandar-log, that you can do that---but when
it comes to show it, you escape with same talk.
We are waiting for your report about your homework.
Here is a very simple FIFO queue:
_______________________________________________________________________
#include <cstdio>
#include <deque>
#include <pthread.h>
class mutex {
friend class cond;
pthread_mutex_t m_mtx;
public:
class guard {
friend class cond;
mutex& m_mtx;
public:
guard(mutex& mtx) : m_mtx(mtx) { m_mtx.lock(); }
~guard() throw() { m_mtx.unlock(); }
};
mutex() {
pthread_mutex_init(&m_mtx, NULL);
}
~mutex() throw() {
pthread_mutex_destroy(&m_mtx);
}
void lock() throw() {
pthread_mutex_lock(&m_mtx);
}
void unlock() throw() {
pthread_mutex_unlock(&m_mtx);
}
};
class cond {
pthread_cond_t m_cond;
public:
cond() {
pthread_cond_init(&m_cond, NULL);
}
~cond() throw() {
pthread_cond_destroy(&m_cond);
}
void wait(mutex::guard& lock) throw() {
pthread_cond_wait(&m_cond, &lock.m_mtx.m_mtx);
}
void signal() throw() {
pthread_cond_signal(&m_cond);
}
void broadcast() throw() {
pthread_cond_broadcast(&m_cond);
}
};
template<typename T>
class fifo {
std::deque<T> m_con;
mutex m_mtx;
cond m_cond;
public:
void push(T const& state) {
mutex::guard lock(m_mtx);
m_con.push_back(state);
}
T& wait_pop() {
mutex::guard lock(m_mtx);
while (m_con.empty()) { m_cond.wait(lock); }
T& state = m_con.front();
m_con.pop_front();
return state;
}
bool try_pop(T& state) {
mutex::guard lock(m_mtx);
if (m_con.empty()) { return false; }
state = m_con.front();
m_con.pop_front();
return true;
}
};
int main() {
{
int i;
fifo<int> numbers;
for (i = 0; i < 10; ++i) {
numbers.push(i);
std::printf("Pushed %d\n", i);
}
std::puts("-----------------------------------------");
while (numbers.try_pop(i)) {
std::printf("Popped %d\n", i);
}
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
std::puts("\n\n____________________________________________\n\
Press <ENTER> to exit...");
std::getchar();
return 0;
}
_______________________________________________________________________