簡體   English   中英

我在 C++ 中遇到類多線程問題

[英]I have a problem with class multithreading in c++

我在 C++ 中遇到類多線程問題,我的代碼是下一個,但我有很多錯誤,我不知道為什么......我希望有人可以幫助我並解釋我的錯誤。

  1. Le fichier Task.hpp :

     #ifndef TASK_HPP_ #define TASK_HPP_ #include "thread.hpp" class Task { public: Task(int id); virtual ~Task(); void setThread(Thread *_thread); void virtual work(); protected: int taskID; Thread *thread; }; #endif /* !TASK_HPP_ */

Le fichier Thread.hpp :

#ifndef THREAD_HPP_
#define THREAD_HPP_

#include "pool.hpp"

class Thread
{
    public:
        Thread();
        void setPool(Pool *_pool);

        void setId(int _id);
        int getId();
        void start();
        void terminate();
        static void* work(void *param);

    private:
        pthread_t pthread;
        Pool *pool;
        int id;
        bool isWorking = false;
};

#endif /* !THREAD_HPP_ */

Le fichier Pool.hpp :

#ifndef POOL_HPP_
#define POOL_HPP_

#include "thread.hpp"
#include "task.hpp"

class Pool
{
    public:
        static pthread_mutex_t taskQueue_lock;
        static pthread_cond_t taskQueue_cond;
        Pool();
        Pool(int num);
        virtual ~Pool();
        void initThreads();
        void assignJob(Task *_job_);
        bool loadJob(Task *&_job_);
        bool end();

    private:
        std::queue<Task*> taskQueue;
        int numOfThreads;
        std::vector<Thread*> threads;
        bool isTerminated = false;
};

#endif /* !POOL_HPP_ */

編譯:

c++  -g -I ./includes/  -c -o sources/pool.o sources/pool.cpp
In file included from sources/pool.cpp:1:
In file included from ./includes/thread.hpp:13:
In file included from ./includes/pool.hpp:14:
./includes/task.hpp:20:24: error: unknown type name 'Thread'; did you mean 'std::thread'?
        void setThread(Thread *_thread);
                       ^~~~~~
                       std::thread
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/thread:285:24: note: 'std::thread' declared here
class _LIBCPP_TYPE_VIS thread
                       ^
In file included from sources/pool.cpp:1:
In file included from ./includes/thread.hpp:13:
In file included from ./includes/pool.hpp:14:
./includes/task.hpp:30:9: error: unknown type name 'Thread'; did you mean 'std::thread'?
        Thread *thread;
        ^~~~~~
        std::thread
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/thread:285:24: note: 'std::thread' declared here
class _LIBCPP_TYPE_VIS thread
                       ^
In file included from sources/pool.cpp:1:
In file included from ./includes/thread.hpp:13:
./includes/pool.hpp:32:28: error: expected expression
        std::vector<Thread*> threads;
                           ^
./includes/pool.hpp:32:21: error: use of undeclared identifier 'Thread'
        std::vector<Thread*> threads;
                    ^
./includes/pool.hpp:33:27: warning: in-class initialization of non-static data member is a C++11 extension [-Wc++11-extensions]
        bool isTerminated = false;
                          ^
In file included from sources/pool.cpp:1:
./includes/thread.hpp:34:24: warning: in-class initialization of non-static data member is a C++11 extension [-Wc++11-extensions]
        bool isWorking = false;
                       ^
2 warnings and 4 errors generated.
make: *** [sources/pool.o] Error 1

您的標頭中存在循環依賴問題。

Task.hpp
   #include "thread.hpp"
      Thread.hpp                <——————————-
         #include "pool.hpp"               ^
             Pool.hpp                      |    Loop
                include "thread.hpp" ——-——->

我認為您應該檢查您的依賴項

您的依賴項是:

task -> thread
thread -> pool
pool -> thread task

你正在編譯池。 池需要線程,線程需要池。 要獲得周圍循環依賴,您需要使用前向聲明。 參見C 循環依賴

#ifndef TASK_HPP_
#define TASK_HPP_


class Thread; // forward declaration


class Task
{
public:
    Task(int id);
    virtual ~Task();
    void setThread(Thread *_thread);

    void virtual work();

protected:
    int taskID;
    Thread *thread;
};

#endif /* !TASK_HPP_ */

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM