尝试等待信号量时出错

时间:2016-09-29 17:03:12

标签: c++ pthreads

我有一个包含信号量的类。执行类run方法。它创建一个线程并传递我的类的函数和对象指针。然后该函数尝试访问对象及其内部的信号量并调用wait。但编译器不允许我这样做。

错误:

// Changing 1.2.3.4 with websites real IP
$website_ip = '1.2.3.4';
curl_setopt($curl, CURLOPT_INTERFACE, $website_ip);

在pthread_create上传入的方法

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

节点和边缘类的基本设计

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;
    }

1 个答案:

答案 0 :(得分:2)

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

您的in班级成员是:

  std::vector<MyEdge*> in;

因此,in[i]MyEdge *

因此,要访问其sem成员,应该是:

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

其他编译错误也是同样的问题。