views:

2924

answers:

3

I have created a boost thread using: boost::thread thrd(&connectionThread); where connectionThread is a simple void function. This works fine, however, when I try to make it wait for some seconds, for example using:

boost::xtime xt;

boost::xtime_get(&xt, boost::TIME_UTC);

xt.sec += 1;

boost::thread::sleep(xt); // Sleep for 1 second

The program crashes at the xtime_get line. Even when manually trying to set xt.sec it doesn't work. I've tried several other methods, but I can't seem to make it work. Is there something I'm doing wrong? Is there a easier way to achieve my goal?

A: 

With that code (not knowing, for example, where you put it), all I can say is that the xtime_get method returns the type of the measure returned. That is, you have to be sure, for example that the following assert holds:

int res = boost::xtime_get(&xt, boost::TIME_UTC);
assert(res == boost::TIME_UTC);

It may happen that in your system this is not the case.

However, looking at the code again, it comes to my mind that the crash may not be related to this call in particular, but with other things you're doing in your application. Again, it depends in where you're using this code. Is it within the operator() of your thread?

Diego Sevilla
+1  A: 

boost::xtime_get() looks like one of the few Boost APIs that's not implemented in a header, so this might be something like not having the Boost library compiled correctly. This is probably somelike having mismatched calling conventions or something. I don't know off the top of my head what steps you might need to go through to rebuild the library - all I've ever used in Boost has been stuff that only requires the headers.

It might be helpful if you just trace into the xtime_get() routine, even if it's at the assembly level. The xtime struct is very, very basic and xtime_get() really doesn't do anything more than call a platform-specific API to get the numbers to plug into the xtime struct.

Michael Burr
+3  A: 
Anonymous
your second way (what i have tryied first) doesnt work... the first one is fine
MiniScalope