in reply to Re^4: Thread-safe modules and callbacks
in thread Thread-safe modules and callbacks

So I really can't see a way of isolating the user from the thread system. We'd need to share every variable at the callback and every variable at the program(to be acesible from the callback) dymanically.

You're saying that you want to share all user code variables with all the threads, but without the user being concerned with the fact that they are shared, nor even knowing that they are using threads at all. Sorry, but that simply isn't possible. Not with any technology I am aware of, and certainly not with Perl.

That said, I think this is a genuine case of the notorious XY problem. That is, you are asking how to implement what you perceive is the solution to the problem, rather than describing the problem and asking how it might be solved.

If you would start by posting a simple worked example of an application, from the user perspective, that talks to two servers (or clients) and demonstrates how you envisage them interacting, then an alternative solution may be apparent. Just describe the application, and how you would like the user visible API to e used to fulfill that application and forget about how it might be implemented. From that we'll see if we can't come up with something that could work for you.


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.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."
  • Comment on Re^5: Thread-safe modules and callbacks

Replies are listed 'Best First'.
Re^6: Thread-safe modules and callbacks
by Saladino (Beadle) on May 10, 2008 at 14:25 UTC
    Ok, here is the way it works at this moment.
    #!/usr/bin/perl use strict; use warnings; use Server; my $Listener = new Server(2222,\&messageHandler); $Listener->Start(); my @messages; push(@messages,"something"); sub messageHandler{ my $message = shift; my $peer = shift; chomp($message); push(@messages,$message); print "Inside callback @messages\n"; } while(1){ print "Outside callback @messages\n"; sleep(1); }
    Start sending messages
    saladino@Zack$ perl pgenerator.pl Listening Outside callback something Outside callback something Outside callback something Outside callback something Outside callback something Outside callback something Outside callback something Outside callback something Outside callback something Inside callback sdfas Outside callback something Inside callback sdfas safsadf Outside callback something Outside callback something Inside callback sdfas safsadf sadfsasafsa Outside callback something Outside callback something Outside callback something
    If i share @messages, it would work, but that doesn't abstract the client from threads

      You're still demonstrating something that shows no advantage to using threads.

      If the users callback is simply going to push inbound messages from a single source onto an array. And the main thread is just going to access them. What is having the point/advantage of using a thread in the first place?

      To be more explicit. What information needs to be shared between which threads? And why?


      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.
        The messages need to be shared, I mean, probably the management of each message will last for too long, so the user can have the main thread continuosly checking the messages to look if something new arrived, it just does that when finishes the last one.
        Apart from that I'm looking for a more generic solution when the user would need to do something with the arrived message before pushing in the array, and for that it'll probably need to use programs variables.
        What i want to say is that the user doesn't know he is using threads so he defines the callback function and uses it like a normal function. Well, that approach is not possible, but something close to that is what I'm looking for.
        Doing this in another language wouldn't be a problem with variables previous to Start() because they are shared by default.