delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2007/04/25/11:53:30

X-Spam-Check-By: sourceware.org
Message-ID: <462F7963.9020701@aaronwl.com>
Date: Wed, 25 Apr 2007 10:53:07 -0500
From: "Aaron W. LaFramboise" <aaronpurpleleaf4 AT aaronwl DOT com>
User-Agent: Thunderbird 1.5.0.10 (Windows/20070221)
MIME-Version: 1.0
To: cygwin AT cygwin DOT com
Subject: Re: Throwing c++ exception across threads
References: <f0no2t$pfq$1 AT sea DOT gmane DOT org>
In-Reply-To: <f0no2t$pfq$1@sea.gmane.org>
X-IsSubscribed: yes
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

Eric Lilja wrote:

> The things is that the connection code may cause an 
> exception and I want to run that in the connection thread so the UI 
> doesn't freeze while it's waiting for an connection attempt to time-out 
> for instance.

Exceptions are a stack unwinding mechanism, and each thread has its own 
stack.  So no, threads are not normally thrown across threads, and if an 
exception 'escapes' from a thread context, it will call 
std::unexpected() and terminate the program, assuming you're using C++. 
  Something similar will happen with SEH or other sorts of exceptions.

A primary reason this effect isn't going to work easily is that it would 
involve the 'main thread' being interrupted asynchronously, while it may 
be in the middle of doing something else, and being unwound by the 
exception handler.  In general, this is not possible; C++ exceptions may 
only be thrown by certain contexts.

There may be various esoteric ways to get what you want, if you really 
wanted it; but the reality is that you almost certainly don't.  The 
strength of exceptions is in unwinding the stack and calling automatic 
destructors.  Using exceptions for general inter-thread communications 
is not appropriate.

As alternatives, I'd suggest:
-Have the main thread wait on the child thread.
-Set a condition variable.
-Release a mutex.

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019