Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well in Java I would call interrupt() on that Thread, the thread would then get an InterruptedException, and would be able to clean up in its exception handler's catch or finally block before exiting.</p> <p>In C there is several options:</p> <ul> <li>send that thread a signal with <code>kill()</code>, and have the signal handler <code>longjmp()</code> to a point in your code where you previously called <code>setjmp()</code>. For cleanup, you'd just do something when <code>setjmp()</code> returns non-zero, meaning it's resuming from the subsequent <code>longjmp()</code> call.</li> <li>call <code>pthread_cancel()</code>. The only real cleanup you get here is that the cancellation handlers you previously registered with <code>pthread_cleanup_push()</code> will get called.</li> <li>have either a volatile variable or a lock protected variable that gets checked periodically (for example once every loop iteration) and set to some value when the calculation should be canceled. Cleanup is easy because you can do whatever you like when the flag is set and you break out of the loop.</li> </ul> <p>I dislike all of them: signals mean you have to handle partial failures (eg. short reads and writes on files and sockets) and <code>errno==EINTR</code> correctly everywhere in your code while avoiding all kinds of gotchas that exist with signal handlers (such as the small stack size and limits on what system calls are safe to use), <code>pthread_cancel()</code> means you have to drag state around between the thread function and the cancellation handlers, and the flag can impact performance since it has to read uncached memory or take a lock every time and if you don't check it often enough then the thread won't respond immediately when the flag is set.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload