This is the mail archive of the libstdc++@sources.redhat.com mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Can queue<...>.empty(), .push(), .front(), .pop() be considered "volatile"?


Say I'm using a queue<...> for a queue of objects passed from one thread
to
another (assume Linux x86, pthreads, glibc2.1.x, gcc 2.95.2,
libstdc++-v3 2.90.3,
etc.) and I use a condition variable for synchronization:

queue<O>		q;
pthread_mutex_t		m;
pthread_cond_t		c;

void	Worker()
{
	pthread_mutex_lock(&m);

	while (true)
	{
		while (q.empty())
		{
			pthread_cond_wait(&c, &m);
		}
		
		O	obj	= queue.front();
		queue.pop();
		
		obj.doSomething();
	}
}

void	Enqueue(const O& obj)
{
	pthread_mutex_lock(&m);
	
	queue.push(obj);
	pthread_cond_signal(&c);
	
	pthread_mutex_unlock(&m);
}

May I safely assume that the internal structure and operations of the
queue
template would safely allow me to use q.empty() in the way I am using it
above?
		
In particular, if I'm using a condition variable such as c to guard
access to
a ring buffer, I declare the read and write pointers into the ring
buffer to be
volatile so that the test in the while loop around pthread_cond_wait()
is 
reliable.

Can q.empty() be used as above without modifying the source to the
queue<>
template to make one or more members volatile?
--
George T. Talbot
<george at moberg dot com>

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]