簡體   English   中英

使用線程的多個緩沖區

[英]multiple buffers using threads

我需要一些算法幫助我正在寫的多線程程序。 它基本上是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;
}

和讀寫線程:

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);
    }

    pthread_exit(0);
}

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

    sem_wait(&sem_full);
    slow_write(thread_data->fd, thread_data->buf.buf, thread_data->buf.numBytes);
    sem_post(&sem_empty);

    pthread_exit(0);
}

所以我的問題是在main中分配給我的threadData變量,在讀寫線程中分配我的信號量邏輯。 我感謝你能給予的任何幫助

您可以使用公共緩沖池,圓形陣列或鏈接列表。 下面是一個Windows示例的zip鏈接,它類似於您所要求的,使用鏈接列表作為線程間消息傳遞系統的一部分來緩沖數據。 除了創建互斥鎖,信號量和寫入線程之外,這些函數既小又簡單。 mtcopy.zip

作為一個不使用文件描述符的windows家伙,我可能錯了in和out,但我認為這需要在你的main中完成才能設置threadData結構。

producerData.fd = in;
consumerData.fd = out;

然后為兩個結構聲明類型為bufType的ONE SINGLE對象。 例如,將threadData的定義更改為

struct threadData {
    int fd;
    bufType* buf;
};

在你的主要,你寫

bufType buffer;
producerData.buf = &buffer;
consumerData.buf = &buffer;

然后兩個線程將使用公共緩沖區。 否則你將寫入producerData緩沖區,但consumerData緩沖區將保持為空(這是你的編寫器線程正在尋找數據的地方)

然后你需要改變你的信號邏輯。 現在你的程序不能接受超過BUFFER_SIZE輸入,因為你的寫線程只會寫一次。 需要圍繞它循環。 然后你需要一些機制來通知作者線程不再發送數據。 例如,你可以這樣做

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);
}

希望沒有更多的錯誤,沒有測試解決方案。 但這個概念應該是你要求的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM