QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 1124|回复: 2

一个"读者写者问题"程序,请帮忙改正,紧急!!

[复制链接]
发表于 2003-5-27 21:21:47 | 显示全部楼层 |阅读模式
以下程序可以运行,但原来我的意图是用读写锁实现的(现在用互斥锁实现),并用pthread_delay_up(&delay);实现线程延时的;用cc a.c -pthread -o a执行(没加注释//的是原来实现的程序);
错误有:1.8:`PTHREAD_RWLOCK_INITIALIZER' undeclared here (not in a function);
2.:11: parse error before '.' token

望高手指正,万分感谢
#include <pthread.h>

pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond=PTHREAD_COND_INITIALIZER;
pthread_mutex_t rwpter=PTHREAD_MUTEX_INITIALIZER;
//pthread_rwlock_t rwpter=PTHREAD_RWLOCK_INITIALIZER;
int count=0;
int iprint=0;
int flag=0;
//struct timespec delay;
//delay.tv_nec=0;

void *reader(void *);
void *writer(void *);
int main()
{
        int i,j;
        pthread_t tid_reader[3];
        pthread_t tid_writer[2];
        //rrwrw
        for(i=0;i<2;i++)
        {
                pthread_create(&tid_reader,NULL,reader,NULL);
        }
        pthread_create(&tid_writer[0],NULL,writer,NULL);
        pthread_create(&tid_reader[2],NULL,reader,NULL);

        for(i=0;i<3;i++)
        {
                pthread_join(tid_reader,NULL);
        }
        for(i=0;i<2;i++)
        {
                pthread_join(tid_writer,NULL);
        }

        exit(0);
}
void *reader(void *arg)
{
        pthread_mutex_lock(&mutex);
        while(flag!=0)
                pthread_cond_wait(&cond,&mutex);
        count++;
        pthread_mutex_unlock(&mutex);

        pthread_mutex_lock(&rwpter);
//        pthread_rwlock_rdlock(&rwpter);
        printf("%d readers are reading,iprint is %d.\n",count,iprint);
//        pthread_rwlock_unlock(&rwpter);
        pthread_mutex_unlock(&rwpter);
        int t=rand()%10;
        printf("The reader will read for %d s.\n",t);
        sleep(t);
//        delay.tv_sec=rand();
//        printf("The reader will read for %d s.\n",delay.tv_sec);
//        pthread_delay_up(&delay);

        pthread_mutex_lock(&mutex);
        count--;
        if(count==0)
                pthread_cond_signal(&cond);
        pthread_mutex_unlock(&mutex);
        printf("a reader is leaving and only %d readers now.\n",count);
        return(NULL);
}

void *writer(void *arg)
{
        pthread_mutex_lock(&mutex);
        flag=1;
        while(count!=0)
        {
                printf("The writer is waiting.\n");
                pthread_cond_wait(&cond,&mutex);
        }

        pthread_mutex_lock(&rwpter);
//        pthread_rwlock_wrlock(&rwpter);
        iprint++;
        printf("a writer is writing while readers %d and iprint is %d.\n",count,iprint);

//        delay.tv_sec=rand();
//        printf("The writer will write for %d s.\n",delay.tv_sec);
//        pthread_delyay_up(&delay);
//        pthread_unlock(&rwpter);
        int t=rand()%10;
        printf("The writer will write for %d s.\n",t);
        sleep(t);
        pthread_mutex_unlock(&rwpter);
       
        flag=0;
        pthread_mutex_unlock(&mutex);
        printf("The writer is leaving.\n");
        pthread_cond_broadcast(&cond);
        return(NULL);
}
发表于 2003-5-28 09:40:26 | 显示全部楼层
add include <stdio.h>
it goes all right in my test
the automake use the " gcc -wall -winplicit -wunused -O1 -g -o testthread main.o -pthread"
回复

使用道具 举报

 楼主| 发表于 2003-5-28 15:47:03 | 显示全部楼层
thank u very very much
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-11-15 19:40 , Processed in 0.080704 second(s), 15 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表