2 r9sf3 r9sf3 于 2013.12.23 09:10 提问

多线程复制程序的求解

我写了一个多线程复制程序遇到下面问题,不知道该怎么解决,求各位大神cp_thread_mutex.c: 在函数‘main’中:
cp_thread_mutex.c:98: 警告:传递‘pthread_mutex_destroy’的第 1 个参数时在不兼容的指针类型间转换
/usr/include/pthread.h:738: 附注:需要类型‘union pthread_mutex_t ’,但实参的类型为‘union pthread_mutex_t **’
cp_thread_mutex.c: 在函数‘reader’中:
cp_thread_mutex.c:116: 警告:传递‘pthread_mutex_lock’的第 1 个参数时在不兼容的指针类型间转换
/usr/include/pthread.h:746: 附注:需要类型‘union pthread_mutex_t ’,但实参的类型为‘union pthread_mutex_t *
cp_thread_mutex.c:128: 警告:传递‘pthread_mutex_unlock’的第 1 个参数时在不兼容的指针类型间转换
/usr/include/pthread.h:757: 附注:需要类型‘union pthread_mutex_t ’,但实参的类型为‘union pthread_mutex_t *
cp_thread_mutex.c: 在函数‘writer’中:
cp_thread_mutex.c:152: 警告:传递‘pthread_mutex_lock’的第 1 个参数时在不兼容的指针类型间转换
/usr/include/pthread.h:746: 附注:需要类型‘union pthread_mutex_t ’,但实参的类型为‘union pthread_mutex_t *
cp_thread_mutex.c:164: 警告:传递‘pthread_mutex_unlock’的第 1 个参数时在不兼容的指针类型间转换
/usr/include/pthread.h:757: 附注:需要类型‘union pthread_mutex_t ’,但实参的类型为‘union pthread_mutex_t *

下面是代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//#include "cp_header.h"
#define BUFFER_SIZE 5
#define THREAD_BUFFER_SIZE 1024
struct prodcons
{
char** buffer;
pthread_mutex_t** lock;
int infd,outfd;
int read_bytes,write_bytes;
int thread_file_size;
int* num;
//pthread_cond_t notempty;
//pthread_cond_t notfull;

};

size_t get_filesize(int fd)
{
struct stat st;
fstat(fd,&st);
return st.st_size;
}

void *reader(void *arg1);
void *writer(void *arg2);
int main(int argc,char *argv[])
{
int infd,outfd;
int k=0;
int res;
pthread_t th_a,th_b;
if(argc!=3) {
exit(1);
}
infd=open(argv[1],O_RDONLY);
outfd=open(argv[2],O_CREAT|O_WRONLY,0644);
if(infd==-1||outfd==-1)
{
printf("fail to open\n");
return -1;
}
else {
printf("open success!!!!!\n");
}
size_t file_size=get_filesize(infd);
printf("file size is %d\n",file_size);

    struct prodcons *b=(struct prodcons *) malloc(BUFFER_SIZE*sizeof(struct prodcons));
    for(int i=0;i<BUFFER_SIZE;i++)
    {
               b->lock[i]=(pthread_mutex_t*)malloc(sizeof(pthread_mutex_t));
               res=pthread_mutex_init(b->lock[i],NULL);
               b->buffer[i]=(char*)malloc(THREAD_BUFFER_SIZE*BUFFER_SIZE);
               b->num[i]=0;
    }

    b->infd=infd;
    b->outfd=outfd;
    b->thread_file_size=file_size;
    printf("file_size is %d\n",b->thread_file_size);

    //pthread_mutex_init(&b->lock, NULL);
    //pthread_cond_init(&b->notempty,NULL);
    //pthread_cond_init(&b->notfull,NULL);
    //b->read_bytes=0;
    //b->write_bytes=0;

    pthread_create(&th_a,NULL,reader,(void *)b);
    pthread_create(&th_b,NULL,writer,(void *)b);

    pthread_join(th_a,NULL);
    pthread_join(th_b,NULL);

    free(b);
    for(int i=0;i<BUFFER_SIZE;i++)
    {
               free(b->lock[i]);
               free(b->buffer[i]);
    }

    close(infd);
    close(outfd);
    for(int i=0;i<BUFFER_SIZE;i++) {
    pthread_mutex_destroy(&b->lock);
    }
    return 0;

}

void *reader(void *arg1)
{
//int read_bytes;
int i=0;
static int count=0;
int ret;
//char buffer[THREAD_BUFFER_SIZE];
struct prodcons *b=(struct prodcons *)arg1;
while(countthread_file_size) {
//pthread_mutex_lock(&b->lock);

              for(i=1;i<BUFFER_SIZE;i++) {
                      pthread_mutex_lock(&b->lock[i]);
                      b->read_bytes=read(b->infd,b->buffer[i],THREAD_BUFFER_SIZE);
                      if((b->read_bytes==-1)&&(errno!=EINTR)) {
                              printf("fail to read\n");
                              break;
                      }
                      else if(b->read_bytes>0) {
                              b->num[i]=b->read_bytes;
                              count+=b->read_bytes;
                              i++;
                              printf("read_bytes is %d\n",b->num[i]);
                      }
                      pthread_mutex_unlock(&b->lock[i]);
                      sleep(1);

             }
             if (i>=BUFFER_SIZE) {
                      i=0;
             }
    }
    /* pthread_mutex_unlock(&b->lock);*/

}

void *writer(void *arg2)
{
struct prodcons *b=(struct prodcons *)arg2;
//char buffer[THREAD_BUFFER_SIZE];
//int write_bytes,data;
int i=0;
int ret1;
static int count1=0;
while(count1thread_file_size) {
//pthread_mutex_lock(&b->lock);

                     for(i=0;i<BUFFER_SIZE;i++) {
                                  if(count1<b->thread_file_size) {
                                                pthread_mutex_lock(&b->lock[i]);
                                                //data=buffer;
                                                b->write_bytes=write(b->outfd,b->buffer[i],b->num[i]);

                                                if((b->write_bytes==-1)&&(errno!=EINTR)) {
                                                             break;
                                                }
                                                else if(b->write_bytes>0) {
                                                             count1+=b->write_bytes;
                                                             i++;
                                                             printf("write_bytes is %d\n",b->write_bytes);
                                                }
                                                pthread_mutex_unlock(&b->lock[i]);
                                                sleep(1);
                                  }
                                  else {
                                              break;
                                  }

                                  //pthread_mutex_unlock(&b->lock);
                                  //sleep(1);


                    }
                    if(i>=BUFFER_SIZE) {
                           i=1;
                    }
                    /*if(pthread_mutex_unlock(&b->lock)!=0) {
                                  perror("pthread_mutex_unlock");
                    }
                    else {
                                  printf("pthread2 unlock the variable\n");
                    }

                    sleep(1);*/
                    //pthread_mutex_destroy(&b->lock);
    }
    /*pthread_cond_signal(&b->notfull);*/
    /*pthread_mutex_unlock(&b->lock);*/

}

1个回答

oyljerry
oyljerry   Ds   Rxr 2015.01.04 22:30

注意参数类型,要传递符合参数类型的变量

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!