Re: Invoking method in other thread context

2008-01-09 Thread Dean Arnold

Shmuel Fomberg wrote:


I read the document of Thread::Apartment, and it's one impressive module.
However, I didn't quite understood what the 'urgent' methods are, and how
they are different from regular ones?


Urgent methods queue their method call requests to the head of the
target object's method request queue, rather than the tail. So they'll get 
serviced
before any other pending requests.


Also, the Thread::Apartment-new is creating objects inside the thread pool?


Sortof. A constructor request is sent to a thread to tell it to install
an object in its thread and start servicing method calls for the object.


It is not clear from the docs. If so, how do I create such object in the
current thread?


Normally, you don't. You install all the apartments, giving them all
proxy references to each other, and then let them all run their own
thread environments, communicating via the proxies. The main thread
then just sits around waiting for threads to exit. However, it is possible
to use the main thread as an object, but it needs to create its own
proxy object to pass to the other apartments.

You might want to review Microsoft's COM/DCOM architecture, and
lookup their definition of apartment threading; its where I got
many of the notions in Thread::Apartment.



Btw, does every access to a shared variable is really catching a global
lock? It's crazy. Why does it do it?


threads::shared is really a tie() module that maps scalar, array, and hash
accesses to a thread-private proxy version of a variable to the real
instance of the shared variable that exists in a global shared Perl interpretter
context. Needless to say, a Perl interpretter
has a *lot* of internal state. And since any reference to the real version
of a variable requires a refcount bump (and eventually, a refcount drop), pretty
much anytime an app touches a shared variable, the shared interpretter has to 
lock
everything down to avoid major internal chaos (esp on multicore systems) due to 
scrambled
internal state. Its similar to the locking needed for many stock C runtime heaps
to avoid scrambled heaps...but those tend to use quick spinlocks, rather than
full blown semaphores (and there are per-thread caching heap managers,
e.g., Hoard and Google's TCMalloc, that try to avoid that locking too)

Thread::Sociable attempts to avoid the refcounting and shared interpretter,
and takes locks only rarely, so it is (and hopefully someday, will be) much 
faster.




But the overhead issues still apply.


Every inter-thread operation is expensive. 


Thanks,
Shmuel.



HTH,
Dean Arnold
Presicient Corp.



RE: Invoking method in other thread context

2008-01-08 Thread Jan Dubois
On Tue, 08 Jan 2008, Shmuel Fomberg wrote:
 I am currently playing with C#. It have a nice feature that one thread can
 call a function in other thread's context. (using the Invoke command, or the
 asynchronic BeginInvoke command)
 This is useful especially in a GUI program, where one thread is handling the
 GUI, and worker threads need to post status.
 
 So how do we do it in Perl?
 Sending signal and relaying the command somehow?

All cross-thread communication with GUI threads should use the Windows message
queue.  Check your GUI library for an interface to the PostMessage() API to
send notifications from the worker thread to the GUI thread.

Cheers,
-Jan




Re: Invoking method in other thread context

2008-01-08 Thread Dean Arnold

Shmuel Fomberg wrote:

Hi All.

I am currently playing with C#. It have a nice feature that one thread can
call a function in other thread's context. (using the Invoke command, or the
asynchronic BeginInvoke command)
This is useful especially in a GUI program, where one thread is handling the
GUI, and worker threads need to post status. 


So how do we do it in Perl?
Sending signal and relaying the command somehow?

Thanks,
Shmuel.




Sorry for the double post...I just realized Thread::Apartment may solve
another part of your issue. It exports proxied method calls between
threads, so assuming thread A is running an instance of class XYZ with
method callme(), and thread B is running an instance of class DEF that
holds a proxy object $threadA to thread A's apartment, then thread B can call
$threadA-callme() directly, ie, no closures needed.

But the overhead issues still apply.

Dean Arnold
Presicient Corp.


Re: Invoking method in other thread context

2008-01-08 Thread Jerry D. Hedden
 one thread can call a function in other thread's context

 So how do we do it in Perl?
 Sending signal and relaying the command somehow?

If you upgrade to the lastest versions of 'threads' and
threads::shared from CPAN, you can send signals to threads.
You can also uses queues to send data using Thread::Queue.