[英]Error when trying to wait on semaphore
我有一個包含信號量的類。 在執行類時運行方法。 它創建一個線程,並傳遞我的類的函數和對象指針。 然后,該函數嘗試訪問該對象及其內部的信號量並調用wait。 但是編譯器不允許我這樣做。
錯誤:
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
在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;
}
節點和邊緣類的基本設計
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( );
};
}
sem_wait(&(node->in[i].sem));
您in
班級成員是:
std::vector<MyEdge*> in;
因此, in[i]
是MyEdge *
。
因此,要訪問其sem
成員,應為:
sem_wait(&(node->in[i]->sem));
其他編譯錯誤是相同的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.