ProcPoll.h
#ifndef TASKQUEUE_H
#define TASKQUEUE_H
#include "hoops_sc/StepTransformerX.h"
#include <functional>
#include <semaphore.h>
#include <cstdio>
#include <unistd.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <pthread.h>
#define PROCESS_NUM 4
#define SHM_SIZE 1024
class ProcPool {
public:
// 获取单例实例的静态方法
static ProcPool *getInstance() {
// 静态局部变量,确保线程安全
static ProcPool instance;
return &instance;
}
void init();
int distributeScTask(const std::string &msg);
// 禁用拷贝构造函数和赋值运算符
ProcPool(const ProcPool &) = delete;
ProcPool &operator=(const ProcPool &) = delete;
// 禁用移动构造函数和移动赋值运算符
ProcPool(ProcPool &&) = delete;
ProcPool &operator=(ProcPool &&) = delete;
static std::map<pid_t, int[2]> procToPipeMap;
private:
// private:
static sem_t *sem;
static sem_t *sem2;
static sem_t *procMutex;
static int *sharePid;
static char *inputFileName;
static char *scDirName;
static int shm_fd[6];
ProcPool();
~ProcPool();
//必须设置成静态
static void procFunc(void *userdata);
static int initSem();
int initProcs();
//保证线程安全
pthread_mutex_t mutex{
};
};
#endif //TASKQUEUE_H
ProcPoll.cpp
#include "procPool.h"
#include <hv/hproc.h>
ProcPool::ProcPool() = default;
ProcPool::~ProcPool() {
sem_destroy(<