#include #include #include #include #include #include #include #define MAX_THREADS 128 static long nloops = 100000000, nthreads = 16; static bool volatile pwait = true; //////////////////////////////////////////////////////////////////////// struct S { S (char const* s) : refs (), value (s) { } unsigned long ref () { return __rw::__rw_atomic_preincrement (refs, false); } unsigned long unref () { return __rw::__rw_atomic_predecrement (refs, false); } char const* get () { #if !defined (NO_LOCK) ref (); #endif // NO_LOCK #if !defined (NO_VIRTUAL_CALL) return do_get (); #else return value; #endif // NO_VIRTUAL_CALL #if !defined (NO_LOCK) unref (); #endif // NO_LOCK } virtual char const* do_get () const; unsigned long refs; char const* value; }; /* virtual */ char const* S::do_get () const { return this->value; } extern "C" { static void* f (void* pv) { S& s = *reinterpret_cast< S* > (pv); unsigned long n = 0; char const* p = 0; while (pwait) ; for (int i = 0; i < nloops; ++i) { p = s.get (); n += strlen (p); for (; p [0]; ++p) n += p [0]; } return (void*)n; } } // extern "C" int main (int argc, char** argv) { switch (argc) { case 3: nloops = atol (argv [2]); case 2: nthreads = atol (argv [1]); break; } pthread_t tid [MAX_THREADS] = { 0 }; if (nthreads > MAX_THREADS) nthreads = MAX_THREADS; printf ("%ld, %ld", nthreads, nloops); pthread_setconcurrency (nthreads); S s ("01234567890123456789"); for (int i = 0; i < nthreads; ++i) { if (pthread_create (tid + i, 0, f, &s)) exit (-1); } sleep (1); pwait = false; for (int i = 0; i < nthreads; ++i) { if (tid [i]) pthread_join (tid [i], 0); } return 0; }