Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: sharing datas between threads/forks (?)

by gamache (Friar)
on Apr 08, 2008 at 13:38 UTC ( [id://678978] : note . print w/replies, xml ) Need Help??


in reply to sharing datas between threads/forks (?)

I want to have the data in memory only once, but having 4 processor cores working with them. Sounds easy, but I haven't found anything so far...

Getting threads to work with shared read/write memory is surprisingly hard. Ask any C++ programmer. Here's a tutorial on how to do it in Perl; it definitely exceeds the scope of a messageboard post. I have never uses Perl threads myself; back when I thought I needed them, Perl threads weren't stable yet, and I haven't actually needed them ever.

In a nutshell, life gets easier when you can use the data 100% read-only. Then you're not limited to threads as the only "easy" solution. You can just as easily whip up a forking version with parent-child communication or even a network server for the data, to which several lightweight forked processes connect.

Replies are listed 'Best First'.
Re^2: sharing datas between threads/forks (?)
by zentara (Archbishop) on Apr 08, 2008 at 14:04 UTC
    Yeah, Perl threads are only useful if you need to share data in realtime between threads, even then then they are slower than forking and using shared memory IPC. BUT, when the data shared is minimal, threads are easier to setup and deal with. Pure C threads work so much better, Perl's handling of threads adds alot of complications. For instance, in C a Perl shared::variable is just a global variable, and memory gets returned to the system when a thread is joined. Threads in Perl can give you the wrong impression of how well threads work in C.... see gdk-threads

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum