Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Multithreading Shared Objects


  • Please log in to reply
31 replies to this topic

#25 DrTom

DrTom

    CC Newcomer

  • Just Joined
  • PipPip
  • 16 posts

Posted 31 January 2011 - 12:32 PM

I put the printf after the call to Child1 to see the effect of calling Child1. Putting the printf before it doesn't accomplish that, or am I missing something?
  • 0

#26 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 31 January 2011 - 02:01 PM

Yes it does, because Child1 modifies it before the printf executes. Try it.
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'


#27 DrTom

DrTom

    CC Newcomer

  • Just Joined
  • PipPip
  • 16 posts

Posted 31 January 2011 - 02:04 PM

BUt I want Child1 to modify it. When done as a single thread, it accomplishes that objective. When done multihreaded, it does not.
  • 0

#28 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 31 January 2011 - 09:15 PM

Yes, I realize that. And the reason why you're getting different values is because you're reading them in the wrong place.
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'


#29 DrTom

DrTom

    CC Newcomer

  • Just Joined
  • PipPip
  • 16 posts

Posted 01 February 2011 - 05:28 AM

That does not make sense. If you comment out the line that says:

printf ("\nSingle thread...\n");

Then you simply repeat the same print statement twice.

The reason I pass a reference to both calls of Child1 is to allow Child1 to modify the content of $TestObj. When I check the value of $TestObj in the main thread after the multithread call, it reverts back to the value it had prior to the call. However, when I do the single thread call, the change is reflected. Child1 simply sets the value of $TestObj->{a} to 2. Both print statements in the main thread should have the value of 2 after both calls.

Edited by DrTom, 01 February 2011 - 06:22 AM.

  • 0

#30 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 01 February 2011 - 11:05 AM

Okay, I figured it out. Take a look at this site: threads::shared - cpan.org
Apparently it's not simple to share stuff allocated by "new".
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'


#31 DrTom

DrTom

    CC Newcomer

  • Just Joined
  • PipPip
  • 16 posts

Posted 01 February 2011 - 11:10 AM

That's my point - you can't use "shared" and "new". However, I don't understand why passing a variable by reference to a subroutine does not have the same effect in a multithreaded scenario vs. a single threaded scenario.
  • 0

#32 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 01 February 2011 - 11:12 AM

I don't either, because doing so would work perfectly fine in C/C++ and other languages I know. Perl is weird like that. There seems to be a workaround near the bottom of the page, though.
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download