如何在 C++ 中创建函数指针队列
How to create a queue of function pointers in C++
我正在尝试使用 C++ 创建协作调度程序,为此我需要一个包含函数指针的队列。
C++ 队列 STL 库在这方面有帮助吗?
似乎我找到了一个足够简单的方法,使用结构来实现函数队列。可能不完美或效率不高,但目前可行。
#include <iostream>
#include <queue>
using namespace std;
struct task {
int id;
void (*fptr) (void);
};
queue<struct task> q;
void fun(void) {
cout<<"Having fun!"<<endl;
}
int main() {
cout<<"Creating a task object"<<endl;
task t;
t.id = 1;
t.fptr = &fun;
cout<<"Calling function directly from object"<<endl;
t.fptr();
cout << "adding the task into the queue"<<endl;
q.push(t);
cout << "calling the function from the queue"<<endl;
task tsk = q.front();
tsk.fptr();
q.pop();
return 0;
}
OUTPUT :
Creating a task object
Calling function directly from object
Having fun!
adding the task into the queue
calling the function from the queue
Having fun!
这显然正是 std::queue
旨在帮助解决的问题类型。
虽然我会改变一些事情。一种是存储 std::function
而不是指向函数的原始指针:
struct task {
int id;
std::function<void()> f;
};
这基本上允许您传递任何可以像函数一样调用的东西,而不仅仅是指向实际函数的指针。对于一个明显的例子,您可以使用 lambda 表达式:
task t { 1, [] {cout << "having fun!\n"; } };
q.push(t);
auto tsk = q.front();
tsk.f();
由于任务几乎唯一可以做的就是调用它,我还考虑为 task
提供一个重载的 operator()
来执行调用:void operator()() { f(); }
,所以如果你只想调用一个任务,你可以这样做:
auto f = q.front();
f();
您的测试程序扩展为包括这些可能看起来更像这样:
#include <iostream>
#include <queue>
#include <functional>
using namespace std;
struct task {
int id;
std::function<void()> f;
void operator()() { f(); }
};
queue<struct task> q;
void fun(void) {
cout << "Having fun!" << endl;
}
int main() {
cout << "Creating a task object" << endl;
task t;
t.id = 1;
t.f = &fun;
cout << "Calling function directly from object" << endl;
t.f();
cout << "adding the task into the queue" << endl;
q.push(t);
cout << "calling the function from the queue" << endl;
task tsk = q.front();
tsk.f();
q.pop();
q.push({ 1, [] {std::cout << "Even more fun\n"; } });
auto t2 = q.front();
t2.f(); // invoke conventionally
t2(); // invoke via operator()
q.pop();
}
我不会使用 std::queue 格式中描述的队列。
它不允许您在任何给定位置插入。
你应该建立自己的。
如果你像下面那样做并添加优先级和名称,那么你可以很容易地想出逻辑来切换任务,switch/update 优先级,甚至插入到所需的位置。
我会说链表总是有速度问题。
您还可以对结构数组执行类似的操作,并使用数组逻辑来移动和插入。
struct function_T {
string name;
uint8_t priority;
void(*func_ptr)(void);
};
struct node_T {
node_T * previous = NULL;
function_T fptr;
node_T * next;
};
int main(void){
function_T task1;
task1.name = "task1";
task1.priority = 1;
task1.func_ptr = func4;
node_T first_node;
first_node.previous = NULL;
first_node.fptr = task1;
first_node.next = NULL;
first_node.fptr.func_ptr();
return 0;
}
我正在尝试使用 C++ 创建协作调度程序,为此我需要一个包含函数指针的队列。
C++ 队列 STL 库在这方面有帮助吗?
似乎我找到了一个足够简单的方法,使用结构来实现函数队列。可能不完美或效率不高,但目前可行。
#include <iostream>
#include <queue>
using namespace std;
struct task {
int id;
void (*fptr) (void);
};
queue<struct task> q;
void fun(void) {
cout<<"Having fun!"<<endl;
}
int main() {
cout<<"Creating a task object"<<endl;
task t;
t.id = 1;
t.fptr = &fun;
cout<<"Calling function directly from object"<<endl;
t.fptr();
cout << "adding the task into the queue"<<endl;
q.push(t);
cout << "calling the function from the queue"<<endl;
task tsk = q.front();
tsk.fptr();
q.pop();
return 0;
}
OUTPUT :
Creating a task object
Calling function directly from object
Having fun!
adding the task into the queue
calling the function from the queue
Having fun!
这显然正是 std::queue
旨在帮助解决的问题类型。
虽然我会改变一些事情。一种是存储 std::function
而不是指向函数的原始指针:
struct task {
int id;
std::function<void()> f;
};
这基本上允许您传递任何可以像函数一样调用的东西,而不仅仅是指向实际函数的指针。对于一个明显的例子,您可以使用 lambda 表达式:
task t { 1, [] {cout << "having fun!\n"; } };
q.push(t);
auto tsk = q.front();
tsk.f();
由于任务几乎唯一可以做的就是调用它,我还考虑为 task
提供一个重载的 operator()
来执行调用:void operator()() { f(); }
,所以如果你只想调用一个任务,你可以这样做:
auto f = q.front();
f();
您的测试程序扩展为包括这些可能看起来更像这样:
#include <iostream>
#include <queue>
#include <functional>
using namespace std;
struct task {
int id;
std::function<void()> f;
void operator()() { f(); }
};
queue<struct task> q;
void fun(void) {
cout << "Having fun!" << endl;
}
int main() {
cout << "Creating a task object" << endl;
task t;
t.id = 1;
t.f = &fun;
cout << "Calling function directly from object" << endl;
t.f();
cout << "adding the task into the queue" << endl;
q.push(t);
cout << "calling the function from the queue" << endl;
task tsk = q.front();
tsk.f();
q.pop();
q.push({ 1, [] {std::cout << "Even more fun\n"; } });
auto t2 = q.front();
t2.f(); // invoke conventionally
t2(); // invoke via operator()
q.pop();
}
我不会使用 std::queue 格式中描述的队列。
它不允许您在任何给定位置插入。 你应该建立自己的。
如果你像下面那样做并添加优先级和名称,那么你可以很容易地想出逻辑来切换任务,switch/update 优先级,甚至插入到所需的位置。
我会说链表总是有速度问题。 您还可以对结构数组执行类似的操作,并使用数组逻辑来移动和插入。
struct function_T {
string name;
uint8_t priority;
void(*func_ptr)(void);
};
struct node_T {
node_T * previous = NULL;
function_T fptr;
node_T * next;
};
int main(void){
function_T task1;
task1.name = "task1";
task1.priority = 1;
task1.func_ptr = func4;
node_T first_node;
first_node.previous = NULL;
first_node.fptr = task1;
first_node.next = NULL;
first_node.fptr.func_ptr();
return 0;
}