octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Thread-safety issues in QtHandles


From: John W. Eaton
Subject: Re: Thread-safety issues in QtHandles
Date: Mon, 31 Oct 2011 12:27:52 -0400

On 31-Oct-2011, Michael Goffioul wrote:

| shared_ptr class actually does internal reference counting, but it
| does it in a thread-safe way (using CPU-atomic operations and volatile
| variables). But then the problem is that the referenced class does not
| have any knowledge of the reference count, which is handled one level
| up, in the shared_ptr class. At the moment, I'm investigating a way to
| solve the problem, using enable_shared_from_this class or weak_ptr
| class.

OK, maybe this is why Jaroslav created the octave_refcount class.  He
never explained that to me.  It might have been that he wanted to move
in this direction but while preserving the interface in which we still
have a count object.

| Here's the diff for the Array class. Obviously, I didn't test those
| changes yet, as I didn't finish the changes (I still need to convert
| octave_value/octave_base_value, which I think is the core of the
| crashes I have in QtHandles). However, the changes compile fine on
| MSVC (and I assume they would also be fine under GCC).

Thanks, seeing the example helped.

Here is a simplified version of what I think we want to do, if
possible.  This example program does not work properly.  See the
comment in foo::foo (foo_rep *new_rep, bool borrow) constructor.  This
constructor is intended to serve the same purpose as the 
octave_value::octave_value (octave_base_value *new_rep, bool borrow)
constructor in the octave_value class.  I think that constructor could
have been used in place of the lines like

  count++
  retval = octave_value (this);

that you see in files like ov-struct.cc.

If it is not possible to grab an extra reference with the shared_ptr
interface, is there some other way to do this and preserve the current
interface in which subsasgn returns an octave_value object?  If so,
could you modify the foo::doit and foo_rep::doit methods in the
example below to show how that would work?

Note that the change you are proposing will affect more than just
code in Octave.  There are some classes in Octave Forge that will be
affected because they also use the count member directly.  I've never
particularly liked the fact that classes had to increment count (or
ask the constructor to increment it), so this may have been bad
design.  I'm not opposed to fixing it, but there will be some
breakage if we delete the count member from the octave_value class.

jwe


#include <iostream>
#include <tr1/memory>
using std::tr1::shared_ptr;

class foo;

class foo_rep
{
public:
  foo doit (void);
};

class foo
{
private:
  shared_ptr<foo_rep> rep;

public:

  foo (void) : rep (new foo_rep ()) { }

  foo (const foo& a) : rep (a.rep) { }

  foo (foo_rep *new_rep, bool borrow)
  {
    // We want to be able to grab an extra reference here.  If we
    // don't, we'll crash on a double free or some other memory
    // corruption error.
    rep = shared_ptr<foo_rep> (new_rep);
  }

  foo& operator = (const foo& a)
  {
    if (this != &a)
      rep = a.rep;

    return *this;
  }

  ~foo (void) { rep.reset (); }

  void show (void)
  {
    std::cerr << "use_count: " << rep.use_count () << std::endl;
  }

  foo doit (void)
  {
    rep->doit ();
    return *this;
  }
};

foo
foo_rep::doit (void)
{
  return foo (this, true);
}

int
main (void)
{
  foo x;
  x.show ();
  foo y = x.doit ();
  x.show ();
  y.show ();
}

reply via email to

[Prev in Thread] Current Thread [Next in Thread]