http://www.perlmonks.org?node_id=324623


in reply to MultiThread possible issues - basic questions - Thanks

With regards to your questions regarding DBI thread safety and code implementations, I would strongly recommend incorporating the creation and destruction of your database handle into your job code - This is because of the behaviour of the DBI in threaded environments. In the interpreter threads (iThreads) environment, the new thread is created through the cloning of the interpreter and assignment of a single interpreter per application thread. Subsequently, as described in the DBI documentation, if the database handle and drivers are loaded and handles created before the thread is created then the new interpreter thread will only receive a cloned copy of the DBI, the drivers and the handles rather than a denovo copy. When this occurs however, the internal pointer data within the handles will refer to the DBI and drivers in the original interpreter, which in turn causes the interpreter thread to croak upon the use of any method call using handles that don't belong to the current thread.

The error messages which you are seeing (Attempt to free unreferenced scalar ...) are in fact the result of the the interpreter thread croaking upon employ of methods within the database driver handle and subsequently attempting to free resources which have been cloned from the parent thread.

In short, DBI handles cannot be shared between interpreter threads and as such each thread should create it's own database handle upon creation.

 

perl -le "print unpack'N', pack'B32', '00000000000000000000001010110111'"