-1

I have a class which contains a semaphore. On execution of the classes run method. It creates a thread and passes a function and an object pointer of my class. The function then tried to access the object and the semaphore inside of it and call wait. But the compiler is not letting me do it.

Error:

myNode.cpp: In function 'void* MyProjectGraph::compute(void*)':
myNode.cpp:70: error: request for member 'sem' in 'node->MyProjectGraph::MyNode::in.std::vector<_Tp, _Alloc>::operator[] [with _Tp = MyProjectGraph::MyEdge*, _Alloc = std::allocator<MyProjectGraph::MyEdge*>](((long unsigned int)i))', which is of non-class type 'MyProjectGraph::MyEdge*'
myNode.cpp:82: error: request for member 'sem' in 'node->MyProjectGraph::MyNode::in.std::vector<_Tp, _Alloc>::operator[] [with _Tp = MyProjectGraph::MyEdge*, _Alloc = std::allocator<MyProjectGraph::MyEdge*>](((long unsigned int)i))', which is of non-class type 'MyProjectGraph::MyEdge*'
myNode.cpp:87: error: request for member 'sem' in 'node->MyProjectGraph::MyNode::out.std::vector<_Tp, _Alloc>::operator[] [with _Tp = MyProjectGraph::MyEdge*, _Alloc = std::allocator<MyProjectGraph::MyEdge*>](((long unsigned int)i))', which is of non-class type 'MyProjectGraph::MyEdge*'
make: *** [myNode.o] Error 1

Method passed in on pthread_create

void *compute(void *ptr){
        MyNode* node = (MyNode*)ptr; 
        time_t end;

        cout<<"Node Running: "<<node->ltr<<endl;

        //wait on all incoming edges
        for(int i = 0; i < node->in.size();i++){
            sem_wait(&(node->in[i].sem));   
            //node->in[i].edgeWait();   
        }

        sleep(node->time);
        sem_wait(&count_sem);
        graphCount += node->value;
        sem_post(&count_sem);
        time(&end);

        //destory dependent semaphores
        for(int i = 0; i < node->in.size();i++){
            sem_destroy(&(node->in[i].sem));        
        }

        //post all outgoing edges
        for(int i = 0; i < node->out.size();i++){
            sem_post(&(node->out[i].sem));  
            //node->out[i].edgePost();          
        }

        printf("Node %c computed a value of %d after %.2lf second.",node->ltr,node->value,difftime(end,start)); 
        pthread_exit(NULL);
        return 0;
    }

Basic design of node and edge classes

class MyNode{
        public: 
            int tid;
            int value;
            int time;
            char ltr;

            pthread_t thread;

            std::vector<MyEdge*> in;
            std::vector<MyEdge*> out;

            MyNode( );
            MyNode(char ltr, int val, int time);
            void addInEdge(MyEdge* edge); 
            void addOutEdge(MyEdge* edge); 
            void run( );
            void signalEdges( );
            void waitEdges( ); //Implementation is not known atm
            void toString( );
    };

 class MyEdge{
        public:
            MyNode* in;
            MyNode* out;

            sem_t sem;

            MyEdge(int init, MyNode* in, MyNode* out);
            int edgeWait( );
            int edgePost( );    
    };
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Qubit
  • 61
  • 6
  • any tips and recommendations would be appreciated.. I'm trying to learn c++, while doing this assignment, so my code might look odd to you. – Qubit Sep 29 '16 at 17:04
  • Rather use c++ standard synchronization mechanisms like a [condition variable](http://en.cppreference.com/w/cpp/thread/condition_variable) – πάντα ῥεῖ Sep 29 '16 at 17:07
  • @πάνταῥεῖ use of semaphores in mandatory in this assignment :\ – Qubit Sep 29 '16 at 17:09
  • This has nothing to do with semaphores. Where is your [MCVE]? You should have spent your own time narrowing down the problem. – Lightness Races in Orbit Sep 29 '16 at 17:32
  • The error messages tell you the problem. `node->in[i]` and `node->out[i]` will return a `MyEdge*` but your dereferencing of `sem` is looking for a reference. Maybe it will be more clear to you if you wrote: `MyEdge* myEdgePtr = node->in[i]; sem_t* sem = &( myEdgePtr->sem );` Do you see why I used `->` where you used `.` in `&(node->in[i].sem)`. – Daniel Wisehart Sep 30 '16 at 03:32

1 Answers1

2
  sem_wait(&(node->in[i].sem));

Your in class member is a:

  std::vector<MyEdge*> in;

Therefore, in[i] is a MyEdge *.

As such, to access its sem member, this should be:

  sem_wait(&(node->in[i]->sem));

The other compilation errors is the same problem.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • 1
    @John Well, we all will get what we deserve regarding our actions :) (says a confessing Buddhist) – πάντα ῥεῖ Sep 29 '16 at 17:17
  • @πάνταῥεῖ: You mean you're destined to be unfairly dupehammered to death, with the wrong duplicate question? – Lightness Races in Orbit Sep 29 '16 at 17:32
  • @LightnessRacesinOrbit That's a thing that can be fixed easily by other dupehammer holders or the community consensus. I don't really think it will affect my _karma_ ;). – πάντα ῥεῖ Sep 29 '16 at 17:46
  • @πάνταῥεῖ: Yes I know that because I have to keep fixing it after you all the time :( – Lightness Races in Orbit Sep 29 '16 at 17:47
  • @Lightness I'll try to improve. Promised! (seems I'm extrapolating too much sometimes, also following _@Queen_'s proposals without closer inspection) – πάντα ῥεῖ Sep 29 '16 at 17:48
  • @Lightness [This kinda stuff](http://stackoverflow.com/questions/39776437/c-stdcout-as-template-argument) will help to balance my karma anyways. – πάντα ῥεῖ Sep 29 '16 at 17:55
  • My only heroic conquest, to date, is the conquest of compiler error messages. "error: request for member 'sem' ...which is of non-class type 'MyProjectGraph::MyEdge*'". The compiler is looking for a member. I can visualize the compiler's code: ok, I have a member operator here, a ".". But my parse tree type is something other than a class. I'll just complain about a "non-class", and, to be nice, pretty-print the type that's in the parse tree. The rest is history. – Sam Varshavchik Sep 29 '16 at 20:51