I am writing a multithreaded Perl script and I want all threads to have access to an instance of the Algorithm::NaiveBayes classifier.
So my code goes like this:
my $nb : shared;
$nb = Algorithm::NaiveBayes->new();
and then $nb gets passed to the subroutine being run by each thread. However, at run time I getting the error about value not valid for shared scalar and it points to the line where I initialize $nb.
I read on other threads that the shared model of Perl can only make shareable simple objects like hash and list, and can only go down one level deep if these are nested.
So, in light of this restriction is it possible to have several threads somehow share an instance of NaiveBayes?
The reason I don't want each thread having separate private copy of $nb is that the trained model is quite large (~1GB) so it's a waste of memory to replicate it N times. The model is only accessed for classification and no changes are made to it (read-only) by any of the threads. So it seems like prime candidate for sharing. But I can't get it to work.
Thanks in advance.