contrapsych
contrapsych

Reputation: 1949

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.

Upvotes: 0

Views: 1362

Answers (2)

Sam Miller
Sam Miller

Reputation: 24164

I tried to reproduce your problem into a smaller example for others. It seems to compile for me using gcc 4.2.1 on Mac OS X 10.6

#include <vector>
#include <boost/thread.hpp>

class Foo
{
public:
    Foo()
    {
        WorkerThreads = new std::vector<boost::thread*>;
    }

private:
    std::vector< boost::thread *> *WorkerThreads;
};

int
main()
{
    const Foo foo;
}

compile & link

g++ -I/opt/local/include -L/opt/local/lib -Wl,-rpath,/opt/local/lib -lboost_thread-mt vector.cc

Upvotes: 0

Howard Hinnant
Howard Hinnant

Reputation: 218700

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.

Upvotes: 1

Related Questions