mirror of
https://github.com/panda3d/panda3d.git
synced 2025-09-30 08:44:19 -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.
|
||||
*/
|
||||
INLINE void GenericThread::
|
||||
set_function(GenericThread::ThreadFunc *function) {
|
||||
_function = function;
|
||||
set_function(std::function<void()> function) {
|
||||
_function = std::move(function);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the function that is called when the thread runs.
|
||||
*/
|
||||
INLINE GenericThread::ThreadFunc *GenericThread::
|
||||
INLINE const std::function<void()> &GenericThread::
|
||||
get_function() const {
|
||||
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 "pnotify.h"
|
||||
|
||||
#ifndef CPPPARSER
|
||||
|
||||
TypeHandle GenericThread::_type_handle;
|
||||
|
||||
/**
|
||||
@ -23,8 +25,6 @@ GenericThread::
|
||||
GenericThread(const std::string &name, const std::string &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(const std::string &name, const std::string &sync_name, GenericThread::ThreadFunc *function, void *user_data) :
|
||||
Thread(name, sync_name),
|
||||
_function(function),
|
||||
_user_data(user_data)
|
||||
_function(std::bind(function, 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::
|
||||
thread_main() {
|
||||
nassertv(_function != nullptr);
|
||||
(*_function)(_user_data);
|
||||
nassertv(_function);
|
||||
_function();
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -17,6 +17,9 @@
|
||||
#include "pandabase.h"
|
||||
#include "thread.h"
|
||||
|
||||
#ifndef CPPPARSER
|
||||
#include <functional>
|
||||
|
||||
/**
|
||||
* A generic thread type that allows calling a C-style thread function without
|
||||
* 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, 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 ThreadFunc *get_function() const;
|
||||
|
||||
INLINE void set_user_data(void *user_data);
|
||||
INLINE void *get_user_data() const;
|
||||
INLINE void set_function(std::function<void()> function);
|
||||
INLINE const std::function<void()> &get_function() const;
|
||||
|
||||
protected:
|
||||
virtual void thread_main();
|
||||
|
||||
private:
|
||||
ThreadFunc *_function;
|
||||
void *_user_data;
|
||||
std::function<void()> _function;
|
||||
|
||||
public:
|
||||
static TypeHandle get_class_type() {
|
||||
@ -61,4 +61,6 @@ private:
|
||||
|
||||
#include "genericThread.I"
|
||||
|
||||
#endif
|
||||
#endif // CPPPARSER
|
||||
|
||||
#endif // GENERICTHREAD_H
|
||||
|
Loading…
x
Reference in New Issue
Block a user