Converting Winthread to Intel tbb

Converting Winthread to Intel tbb

Its possible to convert this code im current using in my program to Intel tbb?

Current using:

thread.h

#ifndef _THREAD_H_
#define _THREAD_H_

#ifdef _WIN32
#include
#define THREAD_RETURN DWORD WINAPI
#else
#define THREAD_RETURN void*
#include
#ifdef _POSIX_THREADS
#include
#else
#error unsupported platform (no pthreads?)
#endif
#endif

// Don't include common.h here as it will break LogManager
#include "CommonTypes.h"
#include
#include

// This may not be defined outside _WIN32
#ifndef _WIN32
#ifndef INFINITE
#define INFINITE 0xffffffff
#endif
#endif

// -----------------------------------------
#ifdef SETUP_TIMER_WAITING
// -----------------
typedef void (*EventCallBack)(void);
#endif
// ----------------------
///////////////////////////////////

namespace Common
{
class CriticalSection
{
#ifdef _WIN32
CRITICAL_SECTION section;
#else
pthread_mutex_t mutex;
#endif
public:

CriticalSection(int spincount = 1000);
~CriticalSection();
void Enter();
bool TryEnter();
void Leave();
};

#ifdef _WIN32
typedef DWORD (WINAPI * ThreadFunc)(void* arg);
#else
typedef void* (*ThreadFunc)(void* arg);
#endif

class Thread
{
public:
Thread(ThreadFunc entry, void* arg);
~Thread();

void SetAffinity(int mask);
static void SetCurrentThreadAffinity(int mask);
#ifdef _WIN32
void WaitForDeath(const int _Wait = INFINITE);
#else
void WaitForDeath();
#endif

private:

#ifdef _WIN32
HANDLE m_hThread;
DWORD m_threadId;
#else
pthread_t thread_id;
#endif
};

class Event
{
public:
Event();

void Init();
void Shutdown();

void Set();
void Wait();
#ifdef _WIN32
void MsgWait();
#else
void MsgWait() {Wait();}
#endif

#ifdef SETUP_TIMER_WAITING
bool TimerWait(EventCallBack WaitCB, int Id = 0, bool OptCondition = true);
bool DoneWait();
void SetTimer();
bool DoneWaiting;
bool StartWait;
int Id;
HANDLE hTimer;
HANDLE hTimerQueue;
#endif

private:
#ifdef _WIN32
HANDLE m_hEvent;
/* If we have waited more than five seconds we can be pretty sure that the thread is deadlocked.
So then we can just as well continue and hope for the best. I could try several times that
this works after a five second timeout (with works meaning that the game stopped and I could
start another game without any noticable problems). But several times it failed to, and ended
with a crash. But it's better than an infinite deadlock. */
static const int THREAD_WAIT_TIMEOUT = 5000; // INFINITE or 5000 for example
#else
bool is_set_;
pthread_cond_t event_;
pthread_mutex_t mutex_;
#endif
};

void InitThreading();
void SleepCurrentThread(int ms);

void SetCurrentThreadName(const char *name);

LONG SyncInterlockedExchangeAdd(LONG *Dest, LONG Val);
LONG SyncInterlockedExchange(LONG *Dest, LONG Val);
LONG SyncInterlockedIncrement(LONG *Dest);

} // namespace Common

#endif

Current thread header

#ifndef _THREAD_H_
#define _THREAD_H_

#ifdef _WIN32
#include
#define THREAD_RETURN DWORD WINAPI
#else
#define THREAD_RETURN void*
#include
#ifdef _POSIX_THREADS
#include
#else
#error unsupported platform (no pthreads?)
#endif
#endif

// Don't include common.h here as it will break LogManager
#include "CommonTypes.h"
#include
#include

// This may not be defined outside _WIN32
#ifndef _WIN32
#ifndef INFINITE
#define INFINITE 0xffffffff
#endif
#endif

#ifdef SETUP_TIMER_WAITING
typedef void (*EventCallBack)(void);
#endif

namespace Common
{
class CriticalSection
{
#ifdef _WIN32
CRITICAL_SECTION section;
#else
pthread_mutex_t mutex;
#endif
public:

CriticalSection(int spincount = 1000);
~CriticalSection();
void Enter();
bool TryEnter();
void Leave();
};

#ifdef _WIN32
typedef DWORD (WINAPI * ThreadFunc)(void* arg);
#else
typedef void* (*ThreadFunc)(void* arg);
#endif

class Thread
{
public:
Thread(ThreadFunc entry, void* arg);
~Thread();

void SetAffinity(int mask);
static void SetCurrentThreadAffinity(int mask);
#ifdef _WIN32
void WaitForDeath(const int _Wait = INFINITE);
#else
void WaitForDeath();
#endif

private:

#ifdef _WIN32
HANDLE m_hThread;
DWORD m_threadId;
#else
pthread_t thread_id;
#endif
};

class Event
{
public:
Event();

void Init();
void Shutdown();

void Set();
void Wait();
#ifdef _WIN32
void MsgWait();
#else
void MsgWait() {Wait();}
#endif

#ifdef SETUP_TIMER_WAITING
bool TimerWait(EventCallBack WaitCB, int Id = 0, bool OptCondition = true);
bool DoneWait();
void SetTimer();
bool DoneWaiting;
bool StartWait;
int Id;
HANDLE hTimer;
HANDLE hTimerQueue;
#endif

private:
#ifdef _WIN32
HANDLE m_hEvent;
static const int THREAD_WAIT_TIMEOUT = 5000;
#else
bool is_set_;
pthread_cond_t event_;
pthread_mutex_t mutex_;
#endif
};

void InitThreading();
void SleepCurrentThread(int ms);

void SetCurrentThreadName(const char *name);

LONG SyncInterlockedExchangeAdd(LONG *Dest, LONG Val);
LONG SyncInterlockedExchange(LONG *Dest, LONG Val);
LONG SyncInterlockedIncrement(LONG *Dest);

}

#endif

Im not expert to try this but i want learn tbb

Thanks

2 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

TBB's strength does not lay in threads the way you are using them, although its called Threading Building Blocks :)
In TBB you'd use TBB::thread objects to do what you are doing, but TBB is more about abstracting threads away and programming in tasks Tasking Building Blocks, might be a better name ;)

The advantage of the TBB approach is the system can easily scale to more processors, or more work load.

If all you need is to simplify your code I'd use boost::thread, it abstracts away implementation details, and most projects already use boost for that matter. But TBB is a great tool if you can restructure your program, but that depends on your needs and timelines.

Leave a Comment

Please sign in to add a comment. Not a member? Join today