Throw an exception into another thread
<p>This is not a duplicate of any question involving catching an exception thrown in one thread from another.</p>

<p>I have code which spawns a thread, and it goes off and does its own thing.</p>

<p>At some point in the future, I'd like to be able to throw one of two exceptions in the other thread (i.e. <code>terminate</code> and <code>interrupt</code>).</p>

<p>One way to do this is to have a variable shared and have the other thread periodically check if it should be killed. My preference is to not have the constraint that it checks (some of the code will be independent of the library doing the threading and won't have been written to check).</p>

<p>In my ideal solution, you may end up in a situation where, debugging, you might find that <code>auto i = 1 + 1</code> throws an exception because another thread told your thread to throw and that's the line you just <em>happened</em> to be on at the time.</p>

<p>Is there a way I can do this?</p>