reSIProcate/rutil  9694
Classes | Functions
testSharedPtr.cxx File Reference
#include <iostream>
#include "rutil/Log.hxx"
#include "rutil/SharedPtr.hxx"
#include "rutil/Data.hxx"
#include "rutil/ThreadIf.hxx"
#include "rutil/Timer.hxx"
#include <unistd.h>
Include dependency graph for testSharedPtr.cxx:

Go to the source code of this file.

Classes

class  Foo
class  TestThread

Functions

void sleepMS (unsigned int ms)
int main ()

Function Documentation

int main ( void  )

Definition at line 77 of file testSharedPtr.cxx.

References resip::ThreadIf::join(), resip::ThreadIf::run(), resip::ThreadIf::shutdown(), sleepMS(), and resip::SharedPtr< T >::use_count().

{
   Log::initialize(Log::Cout, Log::Debug, Data::Empty);
   
   {
      cerr << "!! Test counter logic" << endl;

      SharedPtr<Foo> foo1(new Foo("data"));
      cerr << "foo1 mVal=" << foo1->mVal << " use count=" << foo1.use_count() << endl;
      assert(foo1.use_count() == 1);
      {
         SharedPtr<Foo> foo2(foo1);

         cerr << "foo1 mVal=" << foo1->mVal << " use count=" << foo1.use_count() << endl;
         assert(foo1.use_count() == 2);
         cerr << "foo2 mVal=" << foo2->mVal << " use count=" << foo2.use_count() << endl;
         assert(foo2.use_count() == 2);
      }
      cerr << "foo1 mVal=" << foo1->mVal << " use count=" << foo1.use_count() << endl;
      assert(foo1.use_count() == 1);
   }

   {
      cerr << "!! Test threads" << endl;

      SharedPtr<Foo> foo(new Foo("data"));

      cerr << "foo mVal=" << foo->mVal << " use count=" << foo.use_count() << endl;
      assert(foo.use_count() == 1);

      TestThread thread1(foo);
      TestThread thread2(foo);
      TestThread thread3(foo);
      TestThread thread4(foo);
      TestThread thread5(foo);
      TestThread thread6(foo);
      TestThread thread7(foo);
      TestThread thread8(foo);
      TestThread thread9(foo);
      TestThread thread10(foo);

      cerr << "foo mVal=" << foo->mVal << " use count=" << foo.use_count() << endl;
      assert(foo.use_count() == 11);

      thread1.run();
      thread2.run();
      thread3.run();
      thread4.run();
      thread5.run();
      thread6.run();
      thread7.run();
      thread8.run();
      thread9.run();
      thread10.run();

      sleepMS(500);

      thread1.shutdown();
      thread2.shutdown();
      thread3.shutdown();
      thread4.shutdown();
      thread5.shutdown();
      thread6.shutdown();
      thread7.shutdown();
      thread8.shutdown();
      thread9.shutdown();
      thread10.shutdown();

      thread1.join();
      thread2.join();
      thread3.join();
      thread4.join();
      thread5.join();
      thread6.join();
      thread7.join();
      thread8.join();
      thread9.join();
      thread10.join();

      cerr << "foo mVal=" << foo->mVal << " use count=" << foo.use_count() << endl;
      assert(foo.use_count() == 1);
   }

   cerr << "All OK" << endl;
   return 0;
}

Here is the call graph for this function:

void sleepMS ( unsigned int  ms)

Definition at line 14 of file testSharedPtr.cxx.

{
#ifdef WIN32
   Sleep(ms);
#else
   usleep(ms*1000);
#endif
}