2012-11-27 156 views
1

我正在写一个关于死锁及其检测的代码,我在Ubuntu 12.10,64位上使用eclipse Juno C/C++。 问题是,当我使用C++睡眠线程冲突

睡眠(1)

,我得到当我建立这个

睡眠并没有在此范围内

声明项目,我试图包括

包括< unistd.h>中

,但随后喜欢

在pthread_join

所有的并行线程功能,让我像

未定义的引用在pthread_join错误

,without #include < unistd.h>这样的错误不会显示出来。

示例代码:

#include <unistd.h> 
#include <iostream> 
#include <semaphore.h> 
#include <queue> 
#include <stdlib.h> 

using namespace std; 

pthread_mutex_t mutex; 
sem_t sem; //used for writing in the console 

....... 

void cross(Batman b) { 
// code to check traffic from the right, use counters, condition 
sem_wait(&sem); 
cout << "BAT " << b.num << " from " << b.direction << " crossing" << endl; 
sem_post(&sem); 
sleep(1); 
} 
........ 

P.S.我遵循这些说明,以获得pthreads在其他项目中工作,我也为这个项目做了同样的工作 http://blog.asteriosk.gr/2009/02/15/adding-pthread-to-eclipse-for-using-posix-threads/

p.s.我正在和一位朋友一起工作这个项目,我使用了他使用的相同代码,但仍然得到这些错误,而他并没有

回答

1

当你修复睡眠函数查找问题时,现在你有pthread库问题。

接下来你需要#include <pthread.h>与pthread库链接应用程序

0

这听起来像你有两个不同的错误,

第一sleep()是不确定的,因为你忘了,包括unistd.h中。我还会包含pthread.h,但听起来它可能会被您包含的其中一个头文件所引入。

其次它听起来像你有一个链接器错误添加,你可以编译与-pthread或添加-lpthread链接器。它没有显示的原因是因为链接只能在文件编译完成并且第一个错误阻止了这个时才能完成。我敢打赌,由于某种原因,ld不喜欢-pthread(你是否安装了libpthread-dev?)。您可以尝试将-pthread更改为-lpthread。