C接口实现任务记录

C接口实现任务记录

main—>c接口---->c++接口

由c接口的实现记录任务taskID

//main.cc

# include "c_opt.h"

# include <iostream>

int main() {
    int task1 = 11;
    int task2 = 22;
    int task3 = 33;

	//传入taskid用于区分任务
    c_init(7,task1);
    c_init(56, task2);
    c_init(33, task3);

    c_pushframe(8,task1);
    c_pushframe(9, task3);
    c_pushframe(73, task2);
    
	return 0;
}
//copt.h
# ifndef _COPT_H_
# define _COPT_H_

void c_init(int ct, int taskid);
void c_pushframe(int cpf, int taskid);

# endif
//copt.cc
# include "algo.h"
# include <memory>
# include "copt.h"
# include <map>
# include <iostream>

std::shared_ptr<Algo> algo_;
//记录任务map
std::map<int, std::shared_ptr<Algo>> taskMap_;

void c_init(int ct,int taskid) {
    auto iter = taskMap_.find(taskid);
    if (iter != taskMap_.end()) {
        std::cout << "already inited, do nothing and return!" << std::endl;;
    } else {
        algo_ = std::make_shared<Algo>();
        algo_->init(ct);
        taskMap_.insert(std::pair<int, std::shared_ptr<Algo>>(taskid,algo_));
    }
}

void c_pushframe(int cpf,int taskid) {
    auto iter = taskMap_.find(taskid);
    if (iter != taskMap_.end()) {
        algo_ = iter->second;
        algo_->pushFram(cpf);
    } else {
        std::cout << "error, do init first!" << std::endl;
    }
}

//algo.h
# ifndef _ALGO_H_
# define _ALGO_H_

class Algo {
public:
    Algo();
    ~Algo();
	void init(int param);
	void pushFram(int t);
private:
    int param_;
};

# endif // !_ALGO_H_
//algo.cc
# include <iostream>
# include "algo.h"

Algo::Algo() {
    std::cout << "algo init" << std::endl;
}

Algo::~Algo() {
}

void Algo::init(int param) {
    param_ = param;
    std::cout << "init success" << std::endl;
}

void Algo::pushFram(int t) {
    std::cout << "param_:" << param_ << std::endl;
    std::cout << "input param:" << t << std::endl;
}

编译libalgo.so : g++ algo.cc -o libalgo.so -shared -fPIC

编译libcopt.so : g++ copt.cc -o libcopt.so -std=c++11 -shared -fPIC -L./ -lalgo (此处链接上libalgo.so后,main函数编译时就不需要再去链接了)

编译main函数 : g++ main.cc -o mian.cc -L./ -lcopt

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值