Re: Best Practices For Thread-Safe errno
On 8 Okt., 01:10, Le Chaud Lapin <jaibudu...@gmail.com> wrote:
Like others, I am follwing the general model of letting functions
return true/false to indicate success/failure, then setting an error
to indicate exactly what happened in the case of failure.
I would like to know if there is an portable or semi-portable
equivalent of Microsoft's SetLastError():
http://msdn.microsoft.com/en-us/library/ms680627(VS.85).aspx
Certainly, if a system is multi-threaded, it is not difficult to have
per-thread reserved space to stash the last error. On systems that are
not multi-threaded, no harm would be done.
I'm not sure whether I correctly understand your intentions.
Have you considered boost::thread_specific_ptr</your error_type/>
as the internal representation of this thread_local? is something
along the lines of
-----------------
/your error_type/& your_last_error() {
static boost::thread_specific_ptr</your error_type/> err;
if(err.get() == 0)
err.reset(new /your error_type/());
return *(err.get());
}
// Not necessary, but maybe wanted:
#define YOUR_LAST_ERROR your_last_error()
-----------------
what you would like to have? Alternatively based on the same
idea a getter and setter API could be provided:
-----------------
namespace details {
/your error_type/& your_last_error_impl() {
static boost::thread_specific_ptr</your error_type/> err;
if(err.get() == 0)
err.reset(new /your error_type/());
return *(err.get());
}
}
inline /your error_type/ get_your_last_error() {
return details::your_last_error_impl();
}
inline void set_your_last_error(/your error_type/ value) {
details::your_last_error_impl() = value;
}
-----------------
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]