Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Do you know where your variables are?
 
PerlMonks  

How to make thread-save data on C API

by Tormalon (Initiate)
on Sep 20, 2013 at 12:05 UTC ( #1055007=perlquestion: print w/ replies, xml ) Need Help??
Tormalon has asked for the wisdom of the Perl Monks concerning the following question:

EHLO all,
could you please help to make a shared hashref inside Perl C module?
Further, the value returned by a function should be a multilevel hashref/arrayref.

Part of C code:
SV* myfunc() PREINIT: HV *hash; CODE: hash = newHV(); RETVAL = newRV_noinc((SV*)hash); OUTPUT: RETVAL
Perl code:
#!/usr/bin/perl -w use strict; use warnings; use threads; use threads::shared; use Data::Dumper; use lib qw|. t ../blib/arch ../blib/lib|; use mymodule; use Devel::Peek; use Clone; my $d : shared; $d = mymodule::myfunc();
The output:
Invalid value for shared scalar at ./a.pl line 15.

PS: All functions already done. I need to make the return values "shared for threads". Thanks.

Comment on How to make thread-save data on C API
Select or Download Code
Re: How to make thread-save data on C API
by BrowserUk (Pope) on Sep 20, 2013 at 12:14 UTC

    See SvSHARE(), SvLOCK & SvUNLOCK in perlapi.

    Alternatively, you might do:

    use threads; use threads::shared; my $d : shared; $d = shared_clone( mymodule::myfunc() );

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Thanks for the info. Unfortunately, the SvSHARE does not work for me - I always receive empty HASHREF:
      rt = newHV(); SvSHARE((SV*)rt); sv = newSViv(42); //SvSHARE(sv); hv_stores(rt, "fe", sv); ... ST(i) = sv_2mortal((SV*)newRV_noinc((SV*)rt)); XSRETURN(4); --- result for this tuple: $VAR1 = {};
      So, I have shared HASH, but without any contents inside hash... I'll use shared_clone, but I think I can reproduce/clone the shared_clone source code...
        So, I have shared HASH, but without any contents inside hash

        No. I can't make it work either :(


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1055007]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (9)
As of 2014-04-20 22:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (488 votes), past polls