Hello Monks.

Currently I'm working on a XS/C++ system on linux w/ pthreads. Many of the objects in the system are intended to be shared across perl threads (for various reasons - but mainly memory efficiency).

My question now is: assuming an object is shared, is there any way to know in how many perl threads it's referenced?

This is important to know, because apparently if a shared object goes out of scope in one thread, the default behavior is to call DESTROY() on it, which calls delete(), which makes the object unusable for any other threads that are still running.

Currently I'm doing something like this (ThreadCounted is a base class for objects that are shared but need to be destroyed when they're out of scope in all threads):

pthread_mutex_t ThreadCounted::mutex = PTHREAD_MUTEX_INITIALIZER; unsigned int ThreadCounted::threadcount = 1; ThreadCounted::ThreadCounted() { pthread_mutex_lock(&mutex); threadrefs = ThreadCounted::threadcount; pthread_mutex_unlock(&mutex); } ThreadCounted::~ThreadCounted() { } void ThreadCounted::CLONE() { pthread_mutex_lock(&mutex); ThreadCounted::threadcount++; pthread_mutex_unlock(&mutex); } void ThreadCounted::DESTROY() { pthread_mutex_lock(&mutex); if (--threadrefs == 0) { delete this; } pthread_mutex_unlock(&mutex); }
In other words, I'm assuming the objects are shared across all threads, and the object is only deleted when the last thread has called DESTROY on it.

Also, I'm assuming the class is loaded when there's only one thread running, and the thread count is incremented every time CLONE() is called.

This has some disadvantages, mainly that (AFAIK) I can't tell when a thread is stopped (i.e. there's no hook corresponding to the "inverse" of CLONE). Also it's possible for an object to be shared only by a subset of the threads (again, AFAIK) and I won't be able to tell.

I'd appreciate any insights & comments.

Cheers,
Joost.


In reply to threads, refcounting, XS, DESTROY by Joost

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.