2006-01-17 11:18:38 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// The Loki Library
|
|
|
|
|
// Copyright (c) 2006 Peter K<>mmel
|
|
|
|
|
// Permission to use, copy, modify, distribute and sell this software for any
|
|
|
|
|
// purpose is hereby granted without fee, provided that the above copyright
|
|
|
|
|
// notice appear in all copies and that both that copyright notice and this
|
|
|
|
|
// permission notice appear in supporting documentation.
|
|
|
|
|
// The author makes no representations about the
|
|
|
|
|
// suitability of this software for any purpose. It is provided "as is"
|
|
|
|
|
// without express or implied warranty.
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2006-10-17 20:36:13 +00:00
|
|
|
|
// $Id$
|
|
|
|
|
|
2006-01-17 11:18:38 +00:00
|
|
|
|
|
|
|
|
|
#define LOKI_CLASS_LEVEL_THREADING
|
|
|
|
|
|
2006-01-21 14:13:48 +00:00
|
|
|
|
#ifndef LOKI_CLASS_LEVEL_THREADING
|
|
|
|
|
#define LOKI_OBJECT_LEVEL_THREADING
|
|
|
|
|
#endif
|
|
|
|
|
|
2006-01-17 11:18:38 +00:00
|
|
|
|
#include "Thread.h"
|
|
|
|
|
|
|
|
|
|
#include <loki/LockingPtr.h>
|
|
|
|
|
#include <loki/SafeFormat.h>
|
|
|
|
|
|
|
|
|
|
using namespace Loki;
|
|
|
|
|
|
|
|
|
|
int g;
|
|
|
|
|
int numThreads = 10;
|
|
|
|
|
int loop = 5;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct A
|
|
|
|
|
{
|
2007-05-08 10:48:40 +00:00
|
|
|
|
A(){}
|
2006-01-21 14:13:48 +00:00
|
|
|
|
|
2006-01-17 11:18:38 +00:00
|
|
|
|
#define DO for(int i=0; i<10000000; i++) g++;
|
|
|
|
|
|
2006-01-19 19:36:34 +00:00
|
|
|
|
void print(void* id) const
|
2006-01-17 11:18:38 +00:00
|
|
|
|
{
|
|
|
|
|
DO;Printf("%p: ----------------\n")(id);
|
|
|
|
|
DO;Printf("%p: ---------------\n")(id);
|
|
|
|
|
DO;Printf("%p: --------------\n")(id);
|
|
|
|
|
DO;Printf("%p: -------------\n")(id);
|
|
|
|
|
DO;Printf("%p: ------------\n")(id);
|
|
|
|
|
DO;Printf("%p: -----------\n")(id);
|
|
|
|
|
DO;Printf("%p: ----------\n")(id);
|
|
|
|
|
DO;Printf("%p: ---------\n")(id);
|
|
|
|
|
DO;Printf("%p: --------\n")(id);
|
|
|
|
|
DO;Printf("%p: -------\n")(id);
|
|
|
|
|
DO;Printf("%p: ------\n")(id);
|
|
|
|
|
DO;Printf("%p: -----\n")(id);
|
|
|
|
|
DO;Printf("%p: ----\n")(id);
|
|
|
|
|
DO;Printf("%p: ---\n")(id);
|
|
|
|
|
DO;Printf("%p: --\n")(id);
|
|
|
|
|
DO;Printf("%p: -\n")(id);
|
|
|
|
|
DO;Printf("%p: \n")(id);
|
|
|
|
|
DO;Printf("%p: \n")(id);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2006-09-08 11:08:59 +00:00
|
|
|
|
typedef Loki::LockingPtr<A,LOKI_DEFAULT_MUTEX,DontPropagateConst> UserLockingPtr;
|
|
|
|
|
typedef Loki::LockingPtr<A,LOKI_DEFAULT_MUTEX,PropagateConst> ConstUserLockingPtr;
|
2006-01-17 11:18:38 +00:00
|
|
|
|
|
|
|
|
|
void* RunLocked(void *id)
|
|
|
|
|
{
|
2006-01-19 19:36:34 +00:00
|
|
|
|
volatile A a;
|
2006-01-17 11:18:38 +00:00
|
|
|
|
static Loki::Mutex m;
|
|
|
|
|
for(int i=0; i<loop; i++)
|
|
|
|
|
{
|
2006-09-08 11:08:59 +00:00
|
|
|
|
UserLockingPtr l(a,m);
|
2006-01-17 11:18:38 +00:00
|
|
|
|
l->print(id);
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-19 19:36:34 +00:00
|
|
|
|
void* RunConstLocked(void *id)
|
|
|
|
|
{
|
|
|
|
|
const volatile A a;
|
|
|
|
|
static Loki::Mutex m;
|
|
|
|
|
for(int i=0; i<loop; i++)
|
|
|
|
|
{
|
2006-09-08 11:08:59 +00:00
|
|
|
|
ConstUserLockingPtr l(a,m);
|
2006-01-19 19:36:34 +00:00
|
|
|
|
l->print(id);
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-17 11:18:38 +00:00
|
|
|
|
void* Run(void *id)
|
|
|
|
|
{
|
|
|
|
|
A a;
|
|
|
|
|
for(int i=0; i<loop; i++)
|
|
|
|
|
a.print(id);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main ()
|
|
|
|
|
{
|
|
|
|
|
std::vector<Thread*> threads;
|
|
|
|
|
|
2009-09-26 23:49:05 +00:00
|
|
|
|
Printf("--------------------------------------------------------------------------------------\n");
|
|
|
|
|
|
2006-01-17 11:18:38 +00:00
|
|
|
|
for(int i=0; i<numThreads; i++)
|
|
|
|
|
{
|
|
|
|
|
Printf("Creating thread %d\n")(i);
|
2006-01-21 14:13:48 +00:00
|
|
|
|
threads.push_back(new Thread(RunLocked,reinterpret_cast<void*>(i)));
|
2006-01-17 11:18:38 +00:00
|
|
|
|
}
|
|
|
|
|
for(int i=0; i<numThreads; i++)
|
|
|
|
|
threads.at(i)->start();
|
|
|
|
|
|
|
|
|
|
Thread::JoinThreads(threads);
|
|
|
|
|
Thread::DeleteThreads(threads);
|
|
|
|
|
|
|
|
|
|
Printf("--------------------------------------------------------------------------------------\n");
|
2009-09-26 23:49:05 +00:00
|
|
|
|
|
|
|
|
|
for(int i=0; i<numThreads; i++)
|
|
|
|
|
{
|
|
|
|
|
Printf("Creating thread %d\n")(i);
|
|
|
|
|
threads.push_back(new Thread(RunConstLocked,reinterpret_cast<void*>(i)));
|
|
|
|
|
}
|
|
|
|
|
for(int i=0; i<numThreads; i++)
|
|
|
|
|
threads.at(i)->start();
|
|
|
|
|
|
|
|
|
|
Thread::JoinThreads(threads);
|
|
|
|
|
Thread::DeleteThreads(threads);
|
|
|
|
|
|
2006-01-17 11:18:38 +00:00
|
|
|
|
Printf("--------------------------------------------------------------------------------------\n");
|
|
|
|
|
|
|
|
|
|
for(int i=0; i<numThreads; i++)
|
|
|
|
|
{
|
|
|
|
|
Printf("Creating thread %d\n")(i);
|
2006-01-21 14:13:48 +00:00
|
|
|
|
threads.push_back(new Thread(Run,reinterpret_cast<void*>(i)));
|
2006-01-17 11:18:38 +00:00
|
|
|
|
}
|
|
|
|
|
for(int i=0; i<numThreads; i++)
|
|
|
|
|
threads.at(i)->start();
|
|
|
|
|
|
|
|
|
|
Thread::JoinThreads(threads);
|
|
|
|
|
Thread::DeleteThreads(threads);
|
2006-09-08 11:08:59 +00:00
|
|
|
|
|
2009-09-26 23:49:05 +00:00
|
|
|
|
Printf("--------------------------------------------------------------------------------------\n");
|
2006-10-11 08:51:40 +00:00
|
|
|
|
|
|
|
|
|
// test pair ctor
|
|
|
|
|
volatile A a;
|
|
|
|
|
Loki::Mutex m;
|
|
|
|
|
UserLockingPtr::Pair pair(&a,&m);
|
|
|
|
|
UserLockingPtr l( pair );
|
|
|
|
|
|
|
|
|
|
ConstUserLockingPtr::Pair cpair(&a,&m);
|
|
|
|
|
ConstUserLockingPtr cl( cpair );
|
|
|
|
|
|
2006-01-17 11:18:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|