Now this works as I mentioned before, but when perl_destruct is called all the arenas (I have no clue what an arena is, but apperntly that is where SVs are situated) are destroyed. This means that the SV is destroyed and when another thread tries to use it BOOM segv here we come.
One solution to this is just set PL_perl_destruct_level to 0, but then we leak memory because nothing is cleaned up! This is clearly not a good solution.
My current idea is to take a copy of the main perl interpereter before execution, then use that to create SVs and then return a reference to that SV. That means all shared variables are anonymous and only accessed by a reference.
One day perl will hopefully have my $foo
Ideas could be
Unfourntatly ALL variables that are shared between two threads have to be declared shared, even members of a hashref.
In those cases when you want to pass a variable, I will do Perl_newSVsv(copy_of_main_perl, SV); to create a copy that will live past original interpreter destruction.