Search code examples
c++pointersvectorboost-thread

Creating pointer to Vector of boost::thread pointers


I am attempting to make a vector of pointers to boost thread objects. This vector then is a member of a class that is created on the heap from a pointer when the class constructor is called. It looks something like this.


#ifndef NETWORKSYSTEM_H
#define NETWORKSYSTEM_H
#include "Network.h"
#include "Misc.h"
#include "Enumerators.h"
#include < vector>
#include < boost\thread.hpp>
#include < boost\filesystem.hpp>
#include < string>
#include < iostream>
class NetworkSystem
{
private:
    Status NetworkStatus;
    boost::filesystem3::path *ProjectPath;
    std::string ProjectName;
    //vector for pointers to networks
    std::vector< Network*> *M_Network;
    //Threading Components
    boost::thread *MainThread;
    std::vector< boost::thread *> *WorkerThreads;
    void MainThreadFunction();
    void WorkerThreadFunction();
public:
    NetworkSystem();
    ~NetworkSystem();
    int SetWorkerThreads(int P_WorkerThreads, bool Wait);
    int TotalNetworks();
    int WorkerThreads();
    int PauseAtNetworksCompletion(bool Wait);
    int PauseAtGenerationsCompletion(bool Wait);
};
#endif

// class constructor
NetworkSystem::NetworkSystem()
{
    ProjectPath = new boost::filesystem3::path();
    M_Network = new std::vector< Network*>;
    WorkerThreads = new std::vector< boost::thread*>;
    NetworkStatus = NO_PROJECT_OPEN;
    MainThread = new boost::thread(&NetworkSystem::MainThreadFunction, this);
};

Visual C++ 2010 gives me errors with the boost::thread pointer vector. It underlines WorkerThreads in the constructor and says that "expression must be a modifiable lvalue". I have no problems when doing the same thing with the M_Network vector. If you believe this approach to organizing my worker threads into a vector of pointers so I can initialize and manage them individually is bad, then I suppose I COULD use a thread group, but would like to get this method to work. Any help? Thanks.


Solution

  • I got a similar error as yours:

    class A
    {
        int B;
    public:
        A()
        {
            B = 0;
        }
    
        int B();
    };
    

    Advice: Don't name your member functions the same name as your member data.