mirror of
https://github.com/panda3d/panda3d.git
synced 2025-10-02 09:52:27 -04:00
thread: Allow arbitrary std::function (incl. lambdas) in GenericThread
This commit is contained in:
parent
37b45a010d
commit
b1b05c2be9
@ -15,31 +15,14 @@
|
|||||||
* Replaces the function that is called when the thread runs.
|
* Replaces the function that is called when the thread runs.
|
||||||
*/
|
*/
|
||||||
INLINE void GenericThread::
|
INLINE void GenericThread::
|
||||||
set_function(GenericThread::ThreadFunc *function) {
|
set_function(std::function<void()> function) {
|
||||||
_function = function;
|
_function = std::move(function);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the function that is called when the thread runs.
|
* Returns the function that is called when the thread runs.
|
||||||
*/
|
*/
|
||||||
INLINE GenericThread::ThreadFunc *GenericThread::
|
INLINE const std::function<void()> &GenericThread::
|
||||||
get_function() const {
|
get_function() const {
|
||||||
return _function;
|
return _function;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Replaces the void pointer that is passed to the thread function. This is
|
|
||||||
* any arbitrary pointer; the thread object does no processing on it.
|
|
||||||
*/
|
|
||||||
INLINE void GenericThread::
|
|
||||||
set_user_data(void *user_data) {
|
|
||||||
_user_data = user_data;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the void pointer that is passed to the thread function.
|
|
||||||
*/
|
|
||||||
INLINE void *GenericThread::
|
|
||||||
get_user_data() const {
|
|
||||||
return _user_data;
|
|
||||||
}
|
|
||||||
|
@ -14,6 +14,8 @@
|
|||||||
#include "genericThread.h"
|
#include "genericThread.h"
|
||||||
#include "pnotify.h"
|
#include "pnotify.h"
|
||||||
|
|
||||||
|
#ifndef CPPPARSER
|
||||||
|
|
||||||
TypeHandle GenericThread::_type_handle;
|
TypeHandle GenericThread::_type_handle;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -23,8 +25,6 @@ GenericThread::
|
|||||||
GenericThread(const std::string &name, const std::string &sync_name) :
|
GenericThread(const std::string &name, const std::string &sync_name) :
|
||||||
Thread(name, sync_name)
|
Thread(name, sync_name)
|
||||||
{
|
{
|
||||||
_function = nullptr;
|
|
||||||
_user_data = nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -33,8 +33,17 @@ GenericThread(const std::string &name, const std::string &sync_name) :
|
|||||||
GenericThread::
|
GenericThread::
|
||||||
GenericThread(const std::string &name, const std::string &sync_name, GenericThread::ThreadFunc *function, void *user_data) :
|
GenericThread(const std::string &name, const std::string &sync_name, GenericThread::ThreadFunc *function, void *user_data) :
|
||||||
Thread(name, sync_name),
|
Thread(name, sync_name),
|
||||||
_function(function),
|
_function(std::bind(function, user_data))
|
||||||
_user_data(user_data)
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
GenericThread::
|
||||||
|
GenericThread(const std::string &name, const std::string &sync_name, std::function<void()> function) :
|
||||||
|
Thread(name, sync_name),
|
||||||
|
_function(std::move(function))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,6 +52,8 @@ GenericThread(const std::string &name, const std::string &sync_name, GenericThre
|
|||||||
*/
|
*/
|
||||||
void GenericThread::
|
void GenericThread::
|
||||||
thread_main() {
|
thread_main() {
|
||||||
nassertv(_function != nullptr);
|
nassertv(_function);
|
||||||
(*_function)(_user_data);
|
_function();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
@ -17,6 +17,9 @@
|
|||||||
#include "pandabase.h"
|
#include "pandabase.h"
|
||||||
#include "thread.h"
|
#include "thread.h"
|
||||||
|
|
||||||
|
#ifndef CPPPARSER
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A generic thread type that allows calling a C-style thread function without
|
* A generic thread type that allows calling a C-style thread function without
|
||||||
* having to subclass.
|
* having to subclass.
|
||||||
@ -27,19 +30,16 @@ public:
|
|||||||
|
|
||||||
GenericThread(const std::string &name, const std::string &sync_name);
|
GenericThread(const std::string &name, const std::string &sync_name);
|
||||||
GenericThread(const std::string &name, const std::string &sync_name, ThreadFunc *function, void *user_data);
|
GenericThread(const std::string &name, const std::string &sync_name, ThreadFunc *function, void *user_data);
|
||||||
|
GenericThread(const std::string &name, const std::string &sync_name, std::function<void()> function);
|
||||||
|
|
||||||
INLINE void set_function(ThreadFunc *function);
|
INLINE void set_function(std::function<void()> function);
|
||||||
INLINE ThreadFunc *get_function() const;
|
INLINE const std::function<void()> &get_function() const;
|
||||||
|
|
||||||
INLINE void set_user_data(void *user_data);
|
|
||||||
INLINE void *get_user_data() const;
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void thread_main();
|
virtual void thread_main();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ThreadFunc *_function;
|
std::function<void()> _function;
|
||||||
void *_user_data;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static TypeHandle get_class_type() {
|
static TypeHandle get_class_type() {
|
||||||
@ -61,4 +61,6 @@ private:
|
|||||||
|
|
||||||
#include "genericThread.I"
|
#include "genericThread.I"
|
||||||
|
|
||||||
#endif
|
#endif // CPPPARSER
|
||||||
|
|
||||||
|
#endif // GENERICTHREAD_H
|
||||||
|
Loading…
x
Reference in New Issue
Block a user