Re: is C++ worth it ?
Robert Wessel <robertwessel2@yahoo.com> writes:
On Thu, 02 Aug 2012 17:56:51 -0500, Paavo Helde
<myfirstname@osa.pri.ee> wrote:
Nobody <nobody@nowhere.com> wrote in
news:pan.2012.08.02.15.35.43.804000@nowhere.com:
I'm not suggesting that destructors are a Bad Thing overall, just that
they're a pitfall. If you're going to write non-trivial destructors,
it's especially important that they're thread-safe, exception-safe and
otherwise devoid of surprises.
Thread-safety is easy, in a destructor one can assume no other threads are
trying to access the object any more. If they do, this is a bug (as they
will access a destroyed object), but the destructor cannot do anything
about it, it's too late. So actually the destructor is one piece of code
where one actually does not need to worry about thread-safety at all.
What if the destructor in question is responsible for ending some
threads accessing the object being destroyed?
c_netport::~c_netport(void)
{
ssize_t diag;
n_terminate = true;
if (n_thread != pthread_self()) {
// Trip the port thread off the poll(2) call.
diag = write(n_cmd_pipe[1], &n_terminate, sizeof(n_terminate));
if (diag != sizeof(n_terminate)) {
n_logger->log("Unable to trigger netport thread"
" termination (%zd of %zu): %s\n",
diag, sizeof(n_terminate), strerror(errno));
}
pthread_join(n_thread, NULL);
}
if (n_connect_socket != -1) {
(void) shutdown(n_connect_socket, SHUT_RDWR);
(void) close(n_connect_socket);
n_connect_socket = -1;
}
if (n_listen_socket != -1) {
(void) close(n_listen_socket);
n_listen_socket = -1;
}
(void) close(n_cmd_pipe[0]);
(void) close(n_cmd_pipe[1]);
pthread_mutex_destroy(&n_lock);
pthread_cond_destroy(&n_wait);
}