小编And*_*ton的帖子

C++期货并行处理

我正在使用std::futures并行处理我的算法.我将信息拆分为互斥池,然后在自己的线程中对每个池执行相同的操作.代码如下所示:

class Processor
{
public:
    Processor(const std::string &strVal) : m_strVal(strVal)
    {
    }

    std::string GetVal() const {return m_strVal;}

    std::vector<std::string> Do()
    {
        // do some processing - this can throw an exception
    }

private:
    std::string m_strVal;
};

class ParallelAlgo
{
private:
    std::vector<std::string> m_vecMasterResults;

public:

    ProcessingFunction(const std::vector<std::string> &vecInfo)
    {
        // vecInfo holds mutually exclusive pools

        std::vector<std::future<std::vector<std::string> > > vecFutures(vecInfo.size());

        try
        {
            for (auto n = 0 ; n < vecInfo.size() ; n++)
            {
                vecFuture[n] = std::async(std::launch::async, &ParallelAlgo::WorkFunc, vecInfo[n].GetVal());
            }

            for (auto …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading

13
推荐指数
1
解决办法
777
查看次数

使用继承来添加功能

我正在使用抽象基类向我的所有类添加日志记录功能。它看起来像这样:

class AbstractLog
{
public:
    virtual ~AbstractLog() = 0;

protected:
    void LogException(const std::string &);

private:
    SingletonLog *m_log;    // defined elsewhere - is a singleton object
};
Run Code Online (Sandbox Code Playgroud)

LogException()方法将文本写入SingletonLog对象中定义的日志文件,然后抛出异常。

然后我将它用作所有后续类的基类(在数百个库/DLL 中可能有数百/数千个)。

这允许我LogException()在通常会抛出异常的地方调用。

我的问题是这是否是好的设计/实践。

PS:我使用继承只是为了向我的所有类添加功能,而不是实现任何类型的多态性。最重要的是,我的所有类都与类具有 is-a 关系的概念AbstractLog是有争议的(每个类都是一个可记录的对象?嗯,是的,我想它们是,但只是因为我已经使它们如此) .

c++ inheritance abstract-class

5
推荐指数
1
解决办法
286
查看次数

标签 统计

c++ ×2

abstract-class ×1

inheritance ×1

multithreading ×1