如何在c++中创建一个函数指针队列

How to create a queue of function pointers in C++

本文关键字:一个 函数 队列 指针 c++ 创建      更新时间:2023-10-16

我正在尝试使用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;
}