add LockPtr test with a Mutex and Thread class. Now it is possible to do some simple cross platform multithreading withhin Loki
git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@491 7ec92016-0320-0410-acc4-a06ded1c099a
This commit is contained in:
parent
00bb8d6ba8
commit
8840afc7dd
5 changed files with 592 additions and 0 deletions
339
test/LockingPtr/LockingPtr.vcproj
Executable file
339
test/LockingPtr/LockingPtr.vcproj
Executable file
|
@ -0,0 +1,339 @@
|
|||
<?xml version="1.0" encoding="Windows-1252"?>
|
||||
<VisualStudioProject
|
||||
ProjectType="Visual C++"
|
||||
Version="8,00"
|
||||
Name="LockingPtr"
|
||||
ProjectGUID="{0DCBE03A-DAC7-4669-B29B-102D8F563736}"
|
||||
RootNamespace="LockingPtr"
|
||||
Keyword="Win32Proj"
|
||||
>
|
||||
<Platforms>
|
||||
<Platform
|
||||
Name="Win32"
|
||||
/>
|
||||
</Platforms>
|
||||
<ToolFiles>
|
||||
</ToolFiles>
|
||||
<Configurations>
|
||||
<Configuration
|
||||
Name="Debug|Win32"
|
||||
OutputDirectory="Debug"
|
||||
IntermediateDirectory="Debug"
|
||||
ConfigurationType="1"
|
||||
CharacterSet="1"
|
||||
>
|
||||
<Tool
|
||||
Name="VCPreBuildEventTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCCustomBuildTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCXMLDataGeneratorTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCWebServiceProxyGeneratorTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCMIDLTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCCLCompilerTool"
|
||||
AdditionalOptions="-wd4996"
|
||||
Optimization="0"
|
||||
AdditionalIncludeDirectories="..\..\include;c:\sandbox\boost"
|
||||
PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE _SECURE_SCL=1"
|
||||
MinimalRebuild="true"
|
||||
BasicRuntimeChecks="3"
|
||||
RuntimeLibrary="3"
|
||||
UsePrecompiledHeader="0"
|
||||
WarningLevel="4"
|
||||
Detect64BitPortabilityProblems="false"
|
||||
DebugInformationFormat="3"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCManagedResourceCompilerTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCResourceCompilerTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCPreLinkEventTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCLinkerTool"
|
||||
AdditionalDependencies="loki.lib"
|
||||
AdditionalLibraryDirectories="..\..\lib"
|
||||
GenerateDebugInformation="true"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCALinkTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCManifestTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCXDCMakeTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCBscMakeTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCFxCopTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCAppVerifierTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCWebDeploymentTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCPostBuildEventTool"
|
||||
/>
|
||||
</Configuration>
|
||||
<Configuration
|
||||
Name="Release|Win32"
|
||||
OutputDirectory="Release"
|
||||
IntermediateDirectory="Release"
|
||||
ConfigurationType="1"
|
||||
CharacterSet="1"
|
||||
>
|
||||
<Tool
|
||||
Name="VCPreBuildEventTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCCustomBuildTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCXMLDataGeneratorTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCWebServiceProxyGeneratorTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCMIDLTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCCLCompilerTool"
|
||||
Optimization="2"
|
||||
AdditionalIncludeDirectories="..\..\include"
|
||||
PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE"
|
||||
UsePrecompiledHeader="0"
|
||||
WarningLevel="4"
|
||||
Detect64BitPortabilityProblems="false"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCManagedResourceCompilerTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCResourceCompilerTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCPreLinkEventTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCLinkerTool"
|
||||
LinkIncremental="1"
|
||||
GenerateDebugInformation="true"
|
||||
SubSystem="1"
|
||||
OptimizeReferences="2"
|
||||
EnableCOMDATFolding="2"
|
||||
TargetMachine="1"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCALinkTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCManifestTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCXDCMakeTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCBscMakeTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCFxCopTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCAppVerifierTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCWebDeploymentTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCPostBuildEventTool"
|
||||
/>
|
||||
</Configuration>
|
||||
</Configurations>
|
||||
<References>
|
||||
</References>
|
||||
<Files>
|
||||
<Filter
|
||||
Name="Header Files"
|
||||
Filter="h;hpp;hxx;hm;inl;inc;xsd"
|
||||
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
|
||||
>
|
||||
<File
|
||||
RelativePath="..\..\include\loki\AbstractPimpl.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\include\loki\AssocVector.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\include\loki\DataGenerators.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\include\loki\EmptyType.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\include\loki\Factory.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\include\loki\Function.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\include\loki\Functor.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\include\loki\HierarchyGenerators.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\include\loki\LockingPtr.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\include\loki\LokiTypeInfo.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\include\loki\MultiMethods.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\include\loki\NullType.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\include\loki\Pimpl.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\include\loki\SafeFormat.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\include\loki\ScopeGuard.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\include\loki\Sequence.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\include\loki\Singleton.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\include\loki\SmallObj.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\include\loki\SmartPtr.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\include\loki\static_check.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\include\loki\Threads.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\include\loki\Tuple.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\include\loki\Typelist.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\include\loki\TypelistMacros.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\include\loki\TypeManip.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\include\loki\TypeTraits.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\include\loki\Visitor.h"
|
||||
>
|
||||
</File>
|
||||
<Filter
|
||||
Name="flexstring"
|
||||
>
|
||||
<File
|
||||
RelativePath="..\..\include\loki\flex\allocatorstringstorage.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\include\loki\flex\cowstringopt.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\include\loki\flex\flex_string.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\include\loki\flex\flex_string_details.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\include\loki\flex\flex_string_shell.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\include\loki\flex\simplestringstorage.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\include\loki\flex\smallstringopt.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\include\loki\flex\vectorstringstorage.h"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
</Filter>
|
||||
<Filter
|
||||
Name="Source Files"
|
||||
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
|
||||
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
|
||||
>
|
||||
<File
|
||||
RelativePath=".\main.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\Thread.h"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
</Files>
|
||||
<Globals>
|
||||
</Globals>
|
||||
</VisualStudioProject>
|
11
test/LockingPtr/Makefile
Executable file
11
test/LockingPtr/Makefile
Executable file
|
@ -0,0 +1,11 @@
|
|||
BIN = main
|
||||
CC = gcc
|
||||
CXXFLAGS = -Wall -O2
|
||||
CPPFLAGS = -I../../include -DNDEBUG
|
||||
LDFLAGS = -L../../lib
|
||||
LDLIBS = -lloki
|
||||
|
||||
.PHONY: build clean
|
||||
build: $(BIN)
|
||||
clean:
|
||||
rm -f $(BIN) $(BIN).exe $(BIN).o
|
126
test/LockingPtr/Thread.h
Executable file
126
test/LockingPtr/Thread.h
Executable file
|
@ -0,0 +1,126 @@
|
|||
////////////////////////////////////////////////////////////////////////////////
|
||||
// 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.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef LOKI_THREAD_H_
|
||||
#define LOKI_THREAD_H_
|
||||
|
||||
#include <loki/Threads.h>
|
||||
#include <vector>
|
||||
|
||||
#if defined(_WIN32)
|
||||
|
||||
#include <process.h>
|
||||
|
||||
typedef unsigned int (WINAPI*ThreadFunction_)(void *);
|
||||
|
||||
#define LOKI_pthread_t HANDLE
|
||||
|
||||
#define LOKI_pthread_create(handle,attr,func,arg) \
|
||||
(int)((*handle=(HANDLE) _beginthreadex (NULL,0,(ThreadFunction_)func,arg,0,NULL))==NULL)
|
||||
|
||||
#define LOKI_pthread_join(thread, result) \
|
||||
((WaitForSingleObject((thread),INFINITE)!=WAIT_OBJECT_0) || !CloseHandle(thread))
|
||||
|
||||
#else
|
||||
|
||||
#define LOKI_pthread_t \
|
||||
pthread_t
|
||||
#define LOKI_pthread_create(handle,attr,func,arg) \
|
||||
pthread_create(handle,attr,func,arg)
|
||||
#define LOKI_pthread_join(thread, result) \
|
||||
pthread_join(thread, result)
|
||||
|
||||
#endif
|
||||
|
||||
namespace Loki
|
||||
{
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// \class Thread
|
||||
//
|
||||
// \ingroup ThreadingGroup
|
||||
// Very simple Thread class
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
class Thread
|
||||
{
|
||||
public:
|
||||
|
||||
typedef void* (*ThreadFunction)(void *);
|
||||
|
||||
Thread(ThreadFunction func, void* parm)
|
||||
{
|
||||
func_ = func;
|
||||
parm_ = parm;
|
||||
}
|
||||
|
||||
int start()
|
||||
{
|
||||
return LOKI_pthread_create(&pthread_, NULL, func_, parm_);
|
||||
}
|
||||
|
||||
static int WaitForThread(const Thread& thread)
|
||||
{
|
||||
int status = 0;
|
||||
(void) status;
|
||||
return LOKI_pthread_join(thread.pthread_, (void **)&status);
|
||||
}
|
||||
|
||||
static void JoinThreads(const std::vector<Thread*>& threads)
|
||||
{
|
||||
int status = 0;
|
||||
(void) status;
|
||||
for(size_t i=0; i<threads.size(); i++)
|
||||
WaitForThread(*threads.at(i));
|
||||
}
|
||||
|
||||
static void DeleteThreads(std::vector<Thread*>& threads)
|
||||
{
|
||||
for(size_t i=0; i<threads.size(); i++)
|
||||
{
|
||||
delete threads.at(i);
|
||||
}
|
||||
threads.clear();
|
||||
}
|
||||
|
||||
private:
|
||||
LOKI_pthread_t pthread_;
|
||||
ThreadFunction func_;
|
||||
void* parm_;
|
||||
};
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// \class Mutex
|
||||
//
|
||||
// \ingroup ThreadingGroup
|
||||
// A portable Mutex
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
class Mutex
|
||||
{
|
||||
public:
|
||||
Mutex() { LOKI_THREADS_MUTEX_INIT (&mtx_);}
|
||||
~Mutex() { LOKI_THREADS_MUTEX_DELETE(&mtx_);}
|
||||
void Lock() { LOKI_THREADS_MUTEX_LOCK (&mtx_);}
|
||||
void Unlock(){ LOKI_THREADS_MUTEX_UNLOCK(&mtx_);}
|
||||
private:
|
||||
LOKI_THREADS_MUTEX mtx_;
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace Loki
|
||||
|
||||
#endif
|
||||
|
||||
// $Log:
|
110
test/LockingPtr/main.cpp
Executable file
110
test/LockingPtr/main.cpp
Executable file
|
@ -0,0 +1,110 @@
|
|||
////////////////////////////////////////////////////////////////////////////////
|
||||
// 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.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// $Header:
|
||||
|
||||
#define LOKI_CLASS_LEVEL_THREADING
|
||||
|
||||
#include "Thread.h"
|
||||
|
||||
#include <loki/LockingPtr.h>
|
||||
#include <loki/SafeFormat.h>
|
||||
|
||||
using namespace Loki;
|
||||
|
||||
|
||||
int g;
|
||||
int numThreads = 10;
|
||||
int loop = 5;
|
||||
|
||||
|
||||
struct A
|
||||
{
|
||||
#define DO for(int i=0; i<10000000; i++) g++;
|
||||
|
||||
void print(void* 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);
|
||||
DO;Printf("%p: \n")(id);
|
||||
}
|
||||
};
|
||||
|
||||
typedef Loki::LockingPtr<A,Loki::Mutex> LPtr;
|
||||
|
||||
void* RunLocked(void *id)
|
||||
{
|
||||
A a;
|
||||
static Loki::Mutex m;
|
||||
for(int i=0; i<loop; i++)
|
||||
{
|
||||
LPtr l(a,m);
|
||||
l->print(id);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void* Run(void *id)
|
||||
{
|
||||
A a;
|
||||
for(int i=0; i<loop; i++)
|
||||
a.print(id);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main ()
|
||||
{
|
||||
std::vector<Thread*> threads;
|
||||
|
||||
for(int i=0; i<numThreads; i++)
|
||||
{
|
||||
Printf("Creating thread %d\n")(i);
|
||||
threads.push_back(new Thread(RunLocked,(void*)i));
|
||||
}
|
||||
for(int i=0; i<numThreads; i++)
|
||||
threads.at(i)->start();
|
||||
|
||||
Thread::JoinThreads(threads);
|
||||
Thread::DeleteThreads(threads);
|
||||
|
||||
Printf("--------------------------------------------------------------------------------------\n");
|
||||
Printf("--------------------------------------------------------------------------------------\n");
|
||||
Printf("--------------------------------------------------------------------------------------\n");
|
||||
|
||||
for(int i=0; i<numThreads; i++)
|
||||
{
|
||||
Printf("Creating thread %d\n")(i);
|
||||
threads.push_back(new Thread(Run,(void*)i));
|
||||
}
|
||||
for(int i=0; i<numThreads; i++)
|
||||
threads.at(i)->start();
|
||||
|
||||
Thread::JoinThreads(threads);
|
||||
Thread::DeleteThreads(threads);
|
||||
}
|
||||
|
||||
|
6
test/LockingPtr/make.msvc.bat
Executable file
6
test/LockingPtr/make.msvc.bat
Executable file
|
@ -0,0 +1,6 @@
|
|||
|
||||
cl -c -Zm200 -O2 -DNDEBUG -MT -EHsc -GR -W0 -wd4710 -I"." -I"..\..\include" main.cpp
|
||||
|
||||
link /NOLOGO /SUBSYSTEM:CONSOLE /incremental:no /OUT:"main-msvc.exe" ..\..\lib\loki.lib main.obj
|
||||
|
||||
del *.obj
|
Loading…
Reference in a new issue