Saturday, December 22, 2012

Threads and Exceptions

I learned something recently. One should be careful with catch(...). And one should be very careful with pthread_cancel.

The catch-all has a limited uses of course, since you usually know the types of exception to expect. However, there is the rare occasion for its use. What I learned is that you must always re-throw the caught exception. If you don't, pthread_cancel and pthread_exit may break.

The reason is that on Linux, those functions work by throwing a special exception. This exception is special and does not derive from std::exception. You must re-throw it. It is possible to catch this exception alone and re-throw. In order to do that, see this post.

The same logic also implies that when using pthread_cancel, you should not use functions which serve as cancellation points in destructors. In other words, it is best not to use pthread_cancel.

In summary:

  • When using catch(...), always re-throw.
  • Do not use pthread_cancel.

Note: the same does not appear to be true on Solaris.

References: Ulrich Drepper's blogKenneth's blog.

Monday, December 3, 2012

runonce

I sometimes need a process running in the background. Long ago, I wrote a very simple script which runs a command if not already running. Now, it's updated for OS/X.

https://github.com/jeffgarrett/scripts/blob/master/runonce

Sunday, December 2, 2012

MathJax for Blogger

For basic MathJax support in Blogger, all you have to do is edit the HTML of your template and put the following before </head>:
<script type='text/x-mathjax-config'>
MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']],
                     displayMath: [['\\[','\\]'], ['$$','$$']]}});
</script>
<script src='http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML' type='text/javascript'>
</script>
This is all you have to do if you aren't using Blogger's dynamic views. The principal issue with this method is that RSS readers do not render the LaTeX. This was the justification for the LaTeX for Blogger user script. But MathJax looks so much better on a retina screen!

It is possible to have the best of both worlds, and maybe one day I'll blog how...
$$\int_0^1 x^2 dx = \frac{1}{3}$$ Source: checkmyworking.com

Edit: Set your mobile template to "Custom" to pick up the MathJax on mobile.