Jump to content

Multithreading Shared Objects

- - - - -

  • Please log in to reply
31 replies to this topic

#1
DrTom

DrTom

    Newbie

  • Members
  • PipPip
  • 16 posts
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.


#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Do you have locks on the data?
sudo rm -rf /

#3
DrTom

DrTom

    Newbie

  • Members
  • PipPip
  • 16 posts
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.

#4
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
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.
sudo rm -rf /

#5
DrTom

DrTom

    Newbie

  • Members
  • PipPip
  • 16 posts
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.

#6
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Why were you using threads in the first place then? Can you post the relevant code?
sudo rm -rf /

#7
DrTom

DrTom

    Newbie

  • Members
  • PipPip
  • 16 posts
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.


#8
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
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.
sudo rm -rf /

#9
DrTom

DrTom

    Newbie

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

#10
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Yes, but how do you dereference the hash?

Edit: Add use warnings; to your code and see if anything pops up.
sudo rm -rf /

#11
DrTom

DrTom

    Newbie

  • Members
  • PipPip
  • 16 posts
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.

#12
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
You do realize that you're passing a pointer to a pointer, right?
sudo rm -rf /




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users