如何在c++中创建一个函数指针队列
How to create a queue of function pointers in C++
我正在尝试使用c++创建一个合作调度器,为此我需要一个包含函数指针的队列。
c++队列STL库在这种情况下有帮助吗?
这显然正是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 funn"; } });
auto t2 = q.front();
t2.f(); // invoke conventionally
t2(); // invoke via operator()
q.pop();
}
似乎我发现了一个足够简单的方法,使用结构来实现函数队列。这可能不是完美的或有效的,但目前这是可行的。
#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格式中描述的队列。
它不允许您在任何给定位置插入。你应该建立你自己的。
如果你像下面这样做,并添加优先级和名称,那么你可以很容易地想出切换任务的逻辑,切换/更新优先级,甚至插入到所需的位置。
我要说的是,链表总是有速度问题。您也可以对struct数组做类似的事情,并使用数组逻辑来移动和插入。
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++中声明一个函数时,它需要有函数本身的参数吗
- 如何创建一个函数来计算并返回平均值、最大值和最小值
- 一个函数,用于查找字符串1包含字符串2 c++的次数
- 如何将一个类的函数作为另一个类的另一个函数的参数传递
- 编写一个函数以使用 n 百分比的 CPU 使用率
- 将 N-arg 函数包装到另一个函数中
- 如何封装一个函数,以便它只能由同一类中的一个其他函数调用?
- C++(.cpp文件和.h文件)拆分代码并添加一个函数,提取 - 这很容易吗?
- C++从另一个函数退出函数
- 编写一个函数来删除单链表中的节点(尾部除外),仅授予对该节点的访问权限
- 视觉我希望一个函数在另一个函数C++中进行计算
- C ++如何在原始抽象类中创建一个函数,该函数接受派生类的输入
- 在另一个函数 (c++) 中调用变量
- 如何在另一个函数中使用返回值作为参数?
- 如何包装一个函数以适应另一个函数的所需类型
- 创建一个函数的 Python 绑定,返回指向带有 boost 的向量的指针