0

I have written a C++ function to convert a string in markdown format to a string in html format wrapping the C library libmarkdown2 (Discount) on linux:

string markdown2html(const string& markdown)
{
    auto m = mkd_string(&markdown[0], markdown.size(), 0);

    mkd_compile(m, 0);

    char* text;
    int len = mkd_document(m, &text);

    string html(text, text+len);

    mkd_cleanup(m);

    return html;
}

Is this threadsafe? That is, can I safely call this function multiple times in parallel from different thread contexts? Do Discount markdown handles share any global state that could prevent this? Is there any special per-process or per-thread initialization I need to do to prepare for such usage?

Andrew Tomazos
  • 66,139
  • 40
  • 186
  • 319
  • Why ask here instead of the developer? If you are using Ubuntu libmarkdown (http://packages.ubuntu.com/quantal/libmarkdown2), then the C library is Discount (http://www.pell.portland.or.us/~orc/Code/discount/) by David Parsons. His e-mail address is in his resume (http://www.pell.portland.or.us/~orc/resume.html). It would be nice if you posted his answer here too, for others wondering the same thing. – Nominal Animal Oct 17 '12 at 09:11
  • @NominalAnimal: Because an SO user other than the developer may have used the library and confronted this issue before, and also because the question and answer will become public so someone in future may have the same issue and not have to bother David. – Andrew Tomazos Oct 17 '12 at 09:53
  • Agreed, sorry. Honestly, I meant "Please ask the developer". A quick check of the library and the web page did not show any pertinent info either way, which led me to believe contacting the developer would be the best option. The `mkd_initialize()` in `discount-2.1.0/setup.c` definitely looks racy *for the first call* (to for example `mkd_compile()`): if you start two threads without any markdown calls prior, and the two threads do call `mkd_compile()`, there is a race condition. – Nominal Animal Oct 17 '12 at 11:37

1 Answers1

2

As far as I know the only thing that might not be re-entrant in Discount is the mkd_initialize() function, though I did a small redesign in 2.1.{mumble} to try to keep the globals static.

pb2q
  • 58,613
  • 19
  • 146
  • 147