Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用线程的多缓冲区_C++_Multithreading_Unix_System Calls_Thread Synchronization - Fatal编程技术网

C++ 使用线程的多缓冲区

C++ 使用线程的多缓冲区,c++,multithreading,unix,system-calls,thread-synchronization,C++,Multithreading,Unix,System Calls,Thread Synchronization,我需要一些关于我正在编写的多线程程序的算法帮助。它基本上是unix中的cp命令,但有一个读线程和一个写线程。我正在使用信号量进行线程同步。我将缓冲区和线程数据的结构定义为 struct bufType { char buf[BUFFER_SIZE]; int numBytes; }; struct threadData { int fd; bufType buf; }; 和bufType的全局数组。我的主要代码是 int main(int argc, cons

我需要一些关于我正在编写的多线程程序的算法帮助。它基本上是unix中的cp命令,但有一个读线程和一个写线程。我正在使用信号量进行线程同步。我将缓冲区和线程数据的结构定义为

struct bufType {
    char buf[BUFFER_SIZE];
    int numBytes;
};

struct threadData {
    int fd;
    bufType buf;
};
和bufType的全局数组。我的主要代码是

int main(int argc, const char * argv[])
{
    int in, out;
    pthread_t Producer, Consumer;
    threadData producerData, consumerData;

    if (argc != 3)
    {
        cout << "Error: incorrect number of params" << endl;
        exit(0);
    }
    if ((in = open(argv[1], O_RDONLY, 0666)) == -1)
    {
        cout << "Error: cannot open input file" << endl;
        exit(0);
    }
    if ((out = open(argv[2], O_WRONLY | O_CREAT, 0666)) == -1)
    {
        cout << "Cannot create output file" << endl;
        exit(0);
    }

    sem_init(&sem_empty, 0, NUM_BUFFERS);
    sem_init(&sem_full, 0, 0);

    pthread_create (&Producer, NULL, read_thread, (void *) &producerData);
    pthread_create (&Consumer, NULL, write_thread, (void *) &consumerData);

    pthread_join(Producer, NULL);
    pthread_join(Consumer, NULL);

    return 0;
}

所以我的问题是在main中为我的threadData变量分配什么,在读线程和写线程中为我的信号量逻辑分配什么。非常感谢您提供的任何帮助。您可以使用一个公共缓冲池,一个循环数组或一个链接列表。下面是一个指向Windows示例的zip的链接,该示例类似于您所要求的,使用链接列表作为线程间消息传递系统的一部分来缓冲数据。除了创建互斥量、信号量和写线程之外,这些函数都很小且简单

作为一个不使用文件描述符的windows人员,我可能对输入和输出有错误,但我认为这需要在您的main中完成,以便设置threadData结构

producerData.fd = in;
consumerData.fd = out;
然后为两个结构声明一个bufType类型的对象。例如,将threadData的定义更改为

struct threadData {
    int fd;
    bufType* buf;
};
在你的主体中,你写

bufType buffer;
producerData.buf = &buffer;
consumerData.buf = &buffer;
然后两个线程将使用一个公共缓冲区。否则,您将写入producerData缓冲区,但consumerData缓冲区将保持为空(这就是编写器线程查找数据的地方)

然后你需要改变你的信号逻辑。现在,您的程序无法接受超过
缓冲区大小的输入,因为您的写入线程只会写入一次。需要有一个循环围绕它。然后,您需要某种机制来通知写入线程不再发送数据。例如,你可以这样做

void *read_thread(void *data)
{
    threadData *thread_data;
    thread_data = (threadData *) data;

    while((thread_data->buf->numBytes = slow_read(thread_data->fd, thread_data->buf->buf, BUFFER_SIZE)) > 0)
    {
        sem_post(&sem_full);
        sem_wait(&sem_empty);
    }
    sem_post(&sem_full); // Note that thread_data->buf->numBytes <= 0 now

    pthread_exit(0);
}

void *write_thread(void *data)
{
    threadData *thread_data;
    thread_data = (threadData *) data;


    sem_wait(&sem_full);
    while (thread_data->buf->numBytes > 0)
    {
        slow_write(thread_data->fd, thread_data->buf->buf, thread_data->buf->numBytes);
        sem_post(&sem_empty);
        sem_wait(&sem_full);
    }
    pthread_exit(0);
}
void*读取线程(void*数据)
{
线程数据*线程数据;
线程数据=(线程数据*)数据;
而((线程数据->buf->numBytes=slow\u read(线程数据->fd,线程数据->buf->buf,缓冲区大小))>0)
{
sem_post(sem_full)(&sem_full);
sem_wait(&sem_empty);
}
sem_post(&sem_full);//注意线程数据->buf->numBytes buf->numBytes>0)
{
写慢(线程数据->fd,线程数据->基本单位->基本单位,线程数据->基本单位->基本单位->数量单位);
sem_post(&sem_empty);
sem_等待(&sem_已满);
}
pthread_退出(0);
}

希望没有更多的错误,没有测试解决方案。但是这个概念应该是你所要求的。

我想这会让我走上正确的道路。唯一的问题是,我被要求使用多个缓冲区,但我忘了在问题中包括这一点。因此,buftype的数组我不明白如何处理多个缓冲区。两个线程必须共享缓冲区,否则它们无法通信,或者你的意思是有两个缓冲区,一个被写入,另一个被读取,然后它们被交换?基本上,我们试图模拟这个问题;我们有一个共享资源库。读取获取第一个可用的空缓冲区并“产生”,写入获取第一个完整缓冲区并“消耗”它。这个过程是与信号量同步的,它可能像在main中的循环中用数组索引分配线程数据一样简单?我还没有尝试过,因为我还没有让它与一个缓冲区一起工作
void *read_thread(void *data)
{
    threadData *thread_data;
    thread_data = (threadData *) data;

    while((thread_data->buf->numBytes = slow_read(thread_data->fd, thread_data->buf->buf, BUFFER_SIZE)) > 0)
    {
        sem_post(&sem_full);
        sem_wait(&sem_empty);
    }
    sem_post(&sem_full); // Note that thread_data->buf->numBytes <= 0 now

    pthread_exit(0);
}

void *write_thread(void *data)
{
    threadData *thread_data;
    thread_data = (threadData *) data;


    sem_wait(&sem_full);
    while (thread_data->buf->numBytes > 0)
    {
        slow_write(thread_data->fd, thread_data->buf->buf, thread_data->buf->numBytes);
        sem_post(&sem_empty);
        sem_wait(&sem_full);
    }
    pthread_exit(0);
}