【发布时间】:2012-03-25 09:54:38
【问题描述】:
首先提供一些上下文,这是一个涉及信号量的赋值。我们要找到哲学家就餐问题的代码,让它工作,然后进行一些分析和操作。但是,我遇到了一个错误。
原代码取自http://www.math-cs.gordon.edu/courses/cs322/projects/p2/dp/ 使用 C++ 解决方案。
我在 Code::Blocks 中收到的错误是
philosopher.cpp|206|error: 'Philosopher_run' was not declared in this scope|
并且这个错误发生在这一行:
if ( pthread_create( &_id, NULL, (void *(*)(void *)) &Philosopher_run,
this ) != 0 )
我查看了 pthread_create 方法,但无法修复此错误。如果有人可以向我解释如何纠正这个错误,以及为什么会发生这个错误,我将不胜感激。我已尝试仅提供相关代码。
class Philosopher
{
private:
pthread_t _id;
int _number;
int _timeToLive;
public:
Philosopher( void ) { _number = -1; _timeToLive = 0; };
Philosopher( int n, int t ) { _number = n; _timeToLive = t; };
~Philosopher( void ) {};
void getChopsticks( void );
void releaseChopsticks( void );
void start( void );
void wait( void );
friend void Philosopher_run( Philosopher* p );
};
void Philosopher::start( void )
// Start the thread representing the philosopher
{
if ( _number < 0 )
{
cerr << "Philosopher::start(): Philosopher not initialized\n";
exit( 1 );
}
if ( pthread_create( &_id, NULL, (void *(*)(void *)) &Philosopher_run,
this ) != 0 )
{
cerr << "could not create thread for philosopher\n";
exit( 1 );
}
};
void Philosopher_run( Philosopher* philosopher )
【问题讨论】: