1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
////////////////////////////////////////////////////////////////////////////////
// The Loki Library
// Copyright (c) 2006 Peter Kmmel
// 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_
// $Id: Thread.h 761 2006-10-17 20:48:18Z syntheticpp $
#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) \
((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) \
pthread_join(thread, NULL)
#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)
{
return LOKI_pthread_join(thread.pthread_);
}
static void JoinThreads(const std::vector<Thread*>& threads)
{
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_;
};
} // namespace Loki
#endif
|