Files
vcmi/lib/CThreadHelper.h
T

52 lines
1.4 KiB
C++
Raw Normal View History

2011-12-13 21:35:28 +00:00
#pragma once
2009-04-16 11:14:13 +00:00
/*
* CThreadHelper.h, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
2009-04-16 11:14:13 +00:00
*/
2013-06-26 11:18:27 +00:00
typedef std::function<void()> Task;
2009-04-16 11:14:13 +00:00
2011-02-22 11:52:36 +00:00
/// Can assign CPU work to other threads/cores
2011-12-13 21:35:28 +00:00
class DLL_LINKAGE CThreadHelper
2009-04-16 11:14:13 +00:00
{
boost::mutex rtinm;
int currentTask, amount, threads;
std::vector<Task> *tasks;
void processTasks();
public:
2013-06-26 11:18:27 +00:00
CThreadHelper(std::vector<std::function<void()> > *Tasks, int Threads);
2009-04-16 11:14:13 +00:00
void run();
};
2013-06-26 11:18:27 +00:00
template <typename T> inline void setData(T * data, std::function<T()> func)
2009-04-16 11:14:13 +00:00
{
*data = func();
}
2012-06-27 20:44:01 +00:00
void DLL_LINKAGE setThreadName(const std::string &name);
2009-04-16 11:14:13 +00:00
#define GET_DATA(TYPE,DESTINATION,FUNCTION_TO_GET) \
(std::bind(&setData<TYPE>,&DESTINATION,FUNCTION_TO_GET))
2009-04-16 11:14:13 +00:00
#define GET_SURFACE(SUR_DESTINATION, SUR_NAME) \
(GET_DATA \
(SDL_Surface*,SUR_DESTINATION,\
std::function<SDL_Surface*()>(std::bind(&BitmapHandler::loadBitmap,SUR_NAME,true))))
2009-04-16 11:14:13 +00:00
#define GET_DEF(DESTINATION, DEF_NAME) \
(GET_DATA \
(CDefHandler*,DESTINATION,\
std::function<CDefHandler*()>(std::bind(CDefHandler::giveDef,DEF_NAME))))
2009-04-16 11:14:13 +00:00
#define GET_DEF_ESS(DESTINATION, DEF_NAME) \
(GET_DATA \
(CDefEssential*,DESTINATION,\
std::function<CDefEssential*()>(std::bind(CDefHandler::giveDefEss,DEF_NAME))))