However, this means that I cant have a "global" db connection?

Correct. But this is not a restriction of Perl, or Perl's threads. The restriction lies within the DB vendor's libraries.

It's fundemental design requirement with threaded architectures, that APIs should be reentrant, but code that has been ported to threads from a pre-threaded environment frequently takes shortcuts to avoid too much change to legacy code. Often pre-threaded APIs simply do not lend themselves to reentrancy.

The classic example of this is the C function strtok(), which modifies it's argument, retains a library allocated pointer to the next argument, and forgets what delimiter it is using between successive calls. Long deprecated, but still a part of most C-libraries, it was never safe to use, but still is. There are many more hidden gotchas like it.

Guess the solution is to have the db obj in the dbUpdate function ONLY ...

The biggest problem with your design as posted is that you show no indication of how you are going to indicate to the db thread when the other thread is finished modifying the shared variables.

Ie. Even when you move $dbh = DBI->connect( ... ) inside the db thread, you still have to think about the fact that you do not control when your threads run, or when they get interupted.

I appreciate that you are only posting snippets to give a flavour of what you are doing and that you may well have thought about much more than you are showing, but while(1){ is an endless loop that does not bode well. If you have a high level pseudo-code description of your application, or even a wordful description of what you hope to achieve by using threads, it would be easier to advise you.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

In reply to Re^5: mysqlPP has no driver CLONE() function so is unsafe threaded by BrowserUk
in thread mysqlPP has no driver CLONE() function so is unsafe threaded by Ace128

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.