当前位置: 技术问答>linux和unix
帮忙给看个程序!!
来源: 互联网 发布时间:2015-09-19
本文导语: #include #include #include #include #include #include #include #include #include #include #include void reader_function(void); void writer_function(void); char buffer; int buffer_has_item=0; pthread_mutex_t mutex; struct timespec delay; main() { pthread_t reader; d...
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
void reader_function(void);
void writer_function(void);
char buffer;
int buffer_has_item=0;
pthread_mutex_t mutex;
struct timespec delay;
main()
{
pthread_t reader;
delay.tv_sec=2;
delay.tv_nsec=0;
pthread_mutex_init(&mutex,pthread_mutexattr_default);
pthread_create(&reader,pthread_attr_default,(void*)&reader_function,NULL);
writer_function();
}
void writer_function(void)
{
while(1)
{
pthread_mutex_lock(&mutex);
if(buffer_has_item==0)
{
buffer=make_new_item();
buffer_has_item=1;
}
pthread_mutex_unlock(&mutex);
pthread_delay_np(&delay);
}
}
void reader_function(void)
{
while(1)
{
pthread_mutex_lock(&mutex);
if(buffer_has_item==1)
{
consume_item(buffer);
buffer_has_item=0;
}
pthread_mutex_unlock(&mutex);
pthread_delay_np(&delay);
}
}
以上源代码在调式的时候报错以下是调式信息:
thread2.c:24: `pthread_mutexattr_default' undeclared (first use in this function
)
thread2.c:24: (Each undeclared identifier is reported only once
thread2.c:24: for each function it appears in.)
thread2.c:25: `pthread_attr_default' undeclared (first use in this function)
我该如何声明?还是有别的原因?
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
void reader_function(void);
void writer_function(void);
char buffer;
int buffer_has_item=0;
pthread_mutex_t mutex;
struct timespec delay;
main()
{
pthread_t reader;
delay.tv_sec=2;
delay.tv_nsec=0;
pthread_mutex_init(&mutex,pthread_mutexattr_default);
pthread_create(&reader,pthread_attr_default,(void*)&reader_function,NULL);
writer_function();
}
void writer_function(void)
{
while(1)
{
pthread_mutex_lock(&mutex);
if(buffer_has_item==0)
{
buffer=make_new_item();
buffer_has_item=1;
}
pthread_mutex_unlock(&mutex);
pthread_delay_np(&delay);
}
}
void reader_function(void)
{
while(1)
{
pthread_mutex_lock(&mutex);
if(buffer_has_item==1)
{
consume_item(buffer);
buffer_has_item=0;
}
pthread_mutex_unlock(&mutex);
pthread_delay_np(&delay);
}
}
以上源代码在调式的时候报错以下是调式信息:
thread2.c:24: `pthread_mutexattr_default' undeclared (first use in this function
)
thread2.c:24: (Each undeclared identifier is reported only once
thread2.c:24: for each function it appears in.)
thread2.c:25: `pthread_attr_default' undeclared (first use in this function)
我该如何声明?还是有别的原因?
|
虽然不知道什么意思啊,根据程序瞎猜猜,char buffer; buffer=make_new_item(); 应该是生成一个新的字符;consume_item(buffer); 就是消费这个字符呗, delay.tv_sec=2; delay.tv_nsec=0;pthread_delay_np(&delay); 线程延时2秒
pthread_attr_default 没有就用NULL 代替,make_new_item 函数没有,就随便写一个呀,整个程序是展示多线程同步的问题。
pthread_attr_default 没有就用NULL 代替,make_new_item 函数没有,就随便写一个呀,整个程序是展示多线程同步的问题。
|
能给解释一下
buffer=make_new_item();
consume_item(buffer);
pthread_delay_np(&delay);
这3条语句吗?
要解释也要你把这三个函数贴出来再说啊。
buffer=make_new_item();
consume_item(buffer);
pthread_delay_np(&delay);
这3条语句吗?
要解释也要你把这三个函数贴出来再说啊。
|
pthread_mutexattr_default其实是一个宏,就是NULL而已,至少我的FreeBSD里面的pthread.h里面是这样的……
估计你的pthread.h老了……
估计你的pthread.h老了……
|
顶!