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

#1 DrTom

DrTom

    CC Newcomer

  • Just Joined
  • PipPip
  • 16 posts

Posted 18 January 2011 - 04:50 AM

I'm trying to share an object in a multithreaded script. I can't use:

my $x : shared new TestObbj (1);

So, I tried using a pointer to the variable as an argument to the spawning of the thread:

my $Test = new TestObj (1);
my $TestPtr = \$Test;
my $tid1 = new threads(\&Child1, $TestPtr);

When I print out the value of the pointer inside the thread, it is not the same value as in the main thread. What am I doing wrong?

Edited by DrTom, 28 January 2011 - 02:09 PM.

  • 0

#2 dargueta

dargueta

    I chown trolls.

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

Posted 28 January 2011 - 01:59 PM

Do you have locks on the data?
  • 0

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


#3 DrTom

DrTom

    CC Newcomer

  • Just Joined
  • PipPip
  • 16 posts

Posted 28 January 2011 - 02:12 PM

No. The basic problem is that if I pass a pointer via the thread call, it does not give the same result as when I call the same proc in a single thread.
  • 0

#4 dargueta

dargueta

    I chown trolls.

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

Posted 29 January 2011 - 12:25 AM

Yes, that'd be because you're not locking your data. If you're not locking it, then you have two or more threads racing each other for control of the variable, and who knows what you'll get back. With any multithreading project, you must lock data shared between threads.
  • 0

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


#5 DrTom

DrTom

    CC Newcomer

  • Just Joined
  • PipPip
  • 16 posts

Posted 29 January 2011 - 04:13 AM

Not quite. My test program spawned the thread and simply waited for it to complete. There were no other accesses to the data in the meantime.
  • 0

#6 dargueta

dargueta

    I chown trolls.

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

Posted 29 January 2011 - 09:46 AM

Why were you using threads in the first place then? Can you post the relevant code?
  • 0

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


#7 DrTom

DrTom

    CC Newcomer

  • Just Joined
  • PipPip
  • 16 posts

Posted 29 January 2011 - 12:42 PM

I am attaching repro scripts. Just remove the .txt extensions. This is a stripped down prototype of the problem.

The app I have has a hash of objects. I'd like any thread to access and update any member of the hash. The solution I ended up with has a queue and the threads enqueue messages to it and there is a single thread that does all the updating of the hash. I was hoping for something simpler, by sending a pointer to the hash.

Attached Files


Edited by DrTom, 31 January 2011 - 12:02 PM.

  • 0

#8 dargueta

dargueta

    I chown trolls.

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

Posted 29 January 2011 - 01:20 PM

How are you passing the reference to the hash? Is it like this:

my %hash = ();
# put stuff in here
foo(\%hash);

.
.
.
sub foo($)
{
    my $hashref = shift(@_);

    $hashref->{'blah'} = 'blahstring';
}

I don't see any references in your code, so that's why I'm asking.
  • 0

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


#9 DrTom

DrTom

    CC Newcomer

  • Just Joined
  • PipPip
  • 16 posts

Posted 29 January 2011 - 01:36 PM

In my original app, I use a hash. Here, I'm just trying to get it to preserve the pointer.
  • 0

#10 dargueta

dargueta

    I chown trolls.

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

Posted 30 January 2011 - 12:50 AM

Yes, but how do you dereference the hash?

Edit: Add use warnings; to your code and see if anything pops up.
  • 0

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


#11 DrTom

DrTom

    CC Newcomer

  • Just Joined
  • PipPip
  • 16 posts

Posted 30 January 2011 - 03:13 AM

I didn't get that far. As soon as I assigned an object as a member of the shared hash, it errored out. So, I abandoned the idea at that point. The main problem is not the hash - it's the idea of shared objects, and I cannot find any documentation that says this is by design.
  • 0

#12 dargueta

dargueta

    I chown trolls.

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

Posted 30 January 2011 - 06:36 AM

You do realize that you're passing a pointer to a pointer, right?
  • 0

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





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