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

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

Replies are listed 'Best First'.
Re^7: Thread-safe modules and callbacks
by BrowserUk (Patriarch) on May 10, 2008 at 15:03 UTC

    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.
        Doing this in another language wouldn't be a problem with variables previous to Start() because they are shared by default.

        Sure. But the programmer would still need to know they were using threads, because they would need to perform locking and synchronisation before reading or writing any variable. IThreads avoids that need for most variables, because each thread has its own copy. So you only need to concern yourself with synchronising access to the those (usually very few) variables that need to be shared.

        Your lack of specifics about what needs to be shared and why, combined with your insistance that everything should be shared, but the user shouldn't need to know that he's using threads, suggests to me that you're just trying to make some negative point about iThreads, rather than actually write an application.

        If having a go at iThreads is your purpose, your doing a piss poor job. The safety and simplicity of only sharing that data that needs to be shared, is the one point that goes strongly in their favour.

        Good luck.


        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.