|
Boost Users : |
From: Pavel Syomin (syomin_at_[hidden])
Date: 2006-09-20 04:35:35
Hi,
I'm porting custom library from UNIX to Windows and found that some
piece of code work correctly on UNIX and some time segfaulting on
Windows. I think, that problem is on boost::condition::timed_wait()
function. To be sure I write small test:
#include <stdexcept>
#include <iostream>
#include <boost/thread.hpp>
#include <windows.h>
using namespace boost;
using namespace std;
class AsyncQueue {
public:
AsyncQueue() : m_counter(0) {}
void push()
{
mutex::scoped_lock lock(m_mutex);
m_counter++;
m_condition.notify_one();
}
void timed_pop(const xtime &time)
{
mutex::scoped_lock lock(m_mutex);
if(m_counter) {
m_counter--;
return;
}
if(!m_condition.timed_wait(lock, time)) return;
if(!m_counter) throw out_of_range("oops...");
m_counter--;
}
private:
mutex m_mutex;
condition m_condition;
int m_counter;
};
static AsyncQueue queue;
static void start()
{
for(;;) {
queue.push();
Sleep(100);
}
}
int main(int argc, char **argv)
{
thread thread(start);
xtime time;
for(;;) {
xtime_get(&time, TIME_UTC);
time.nsec += 10000;
queue.timed_pop(time);
}
return 0;
}
AsyncQueue is a simple asynchronous queue container with two operations
- push() and timed_pop(). Because this is test, AsyncQueue don't really
collect something, but it's has variable (m_counter field) that count
current number on elements in container. When push() called m_counter
incremented and when timed_pop() "virtually" remove item from queue
m_counter decremented.
In main() I create additional thread, which push data to queue. Data
popped from queue in main thread by calling timed_pop(). But sometimes
timed_pop throw out_of_range exception. If you look in code you can see
that it's mean that condition::timed_wait() return true, but queue is empty!
I look to boost-users list and found e-mail's about similar problems
from Eric Colleu, but decision wasn't found.
Two words about my environment. I use Visual Studio 2005, Windows XP SP2
on VMWare 5.5.1. Boost version is 1.33.1.
Thanks for constructive answers.
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net