繁体   English   中英

Pthread同步:来回读取两个文本文件

[英]Pthread Synchronization: Back & Forth Reading of Two Text Files

我正在编写一个创建两个线程的程序。 每个线程负责读取一个文本文件,每行一个字符。

第一种格式如下:

h
0
h
0
...

第二个格式如下:

0
i
0
i
0
i

有时,可能会有多个字母,或多个零。 但是,可以肯定的是,如果一个文件的一行上有一个字母,则第二个文件的相应行将具有0,反之亦然。

线程应该继续将文件输入读取到全局char数组中,直到达到零为止。 此时,它们允许其他线程接管。 并且它们一直来回移动,直到完全读取了两个文件。

在这一点上,当我跑步时,我得到的变化是(1)许多h后跟许多i,或者(2)( 正确答案 )连续不断地出现hihihi,或者(3)有时许多i后跟许多h。 因此,我知道我的同步方法已关闭。

这是我的一个线程的示例:(请注意,除了要打开的文件以外,两个线程都完全相同。)

void *getMessage1()
{
FILE *studentOne = fopen("Student1", "r");

size_t howManyChars;
char *placeHolderChars; 
int count = 1;
while (count < 501)
{
    placeHolderChars = NULL;
    getline(&placeHolderChars, &howManyChars, studentOne);

    if(strcmp(placeHolderChars, "0\n") == 0) //if we've reached a zero
    {

         pthread_mutex_unlock(&lock); 
    }
    else
    {   while(1)
        {
            if(pthread_mutex_trylock(&lock) == 0)
            {

                break;
            }
        }

        if(strlen(placeHolderChars)>0)
        {
             placeHolderChars[1] = '\0';
        }

        strcat(message,placeHolderChars);
    }

    free(placeHolderChars);

    if(feof(studentOne))
    {

        pthread_mutex_unlock(&lock); //unlock
        fclose(studentOne);
        break;
    }
    count++;

 }

return 0;
}

这是我的主要方法:

int main(void)
{
pthread_t id1;
pthread_t id2;

pthread_create((&id1), NULL, getMessage1, NULL);
pthread_create((&id2), NULL, getMessage2, NULL);

pthread_join(id1, NULL);
pthread_join(id2, NULL);

int j;

for (j = 0; j < 1001; j++) 
{
     printf ("%c ",message[j]);
}

return 0;
}

对于如何更好地使用锁定,解锁,等待和/或信号来创建具有一致结果的有效同步技术的任何指导,我将不胜感激。

这是尝试一种执行所需功能的程序的尝试。 测试不足,但是;)

#include <iostream>
#include <fstream>
#include <string>

#include <cassert>
#include <pthread.h>



using std::cout;
using std::ifstream;
using std::string;

const string FILE1("file1.txt");
const string FILE2("file2.txt");

enum State
{
    UNINITIALIZED,
    THREAD_ONE_READS,
    THREAD_TWO_READS
};

struct ThreadInfo
{
    State state;
    string filename;
};

State state = UNINITIALIZED;
pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

void* thread_func(void* arg)
{
    // Open file 'h'.                                                                                                                                                                                                                                                                                                                                                                                                                  

    ThreadInfo ti = *reinterpret_cast<ThreadInfo*>(arg);
    ifstream infile;
    infile.open (ti.filename.c_str(), std::ifstream::in);

    // while (not EOF)                                                                                                                                                                                                                                                                                                                                                                                                                 
    //    Read 'h' or 'i': until 0 reached. Wake up other thread.                                                                                                                                                                                                                                                                                                                                                                      

    string line;
    getline(infile, line);
    while (infile.good())
    {
        cout << "Thread " << pthread_self() << " read " << line << '\n';
        pthread_mutex_lock(&mut);
        while (state == ti.state)
        {
            pthread_cond_wait(&cond, &mut);
        }
        pthread_mutex_unlock(&mut);

        assert(line.length() == 1);
        if (line[0] == '0')
        {
            pthread_mutex_lock(&mut);
            state = ti.state;
            cout << "Got 0, transferring, setting state to " << state << '\n';
            pthread_cond_signal(&cond);
            pthread_mutex_unlock(&mut);
        }
        else
        {
            cout << "Read char: " << line << '\n';
        }
        getline(infile, line);
    }

    pthread_mutex_lock(&mut);
    state = ti.state;
    cout << "Finishing thread, transferring, setting state to " << state << '\n';
    pthread_cond_signal(&cond);
    pthread_mutex_unlock(&mut);
}

int main()
{
    // Create thread 1                                                                                                                                                                                                                                                                                                                                                                                                                 
    // Create thread 2                                                                                                                                                                                                                                                                                                                                                                                                                 

    pthread_t thread_one_handle;
    pthread_t thread_two_handle;
    state = THREAD_ONE_READS;
    int result;

    ThreadInfo info1 = { THREAD_TWO_READS, FILE1 };
    result = pthread_create(&thread_one_handle, NULL, thread_func, &info1);
    assert(result == 0);

    ThreadInfo info2 = { THREAD_ONE_READS, FILE2 };
    result = pthread_create(&thread_two_handle, NULL, thread_func, &info2);
    assert(result == 0);

    result = pthread_join(thread_one_handle, NULL);
    assert(result == 0);
    result = pthread_join(thread_two_handle, NULL);
    assert(result == 0);
    cout << "main(): joined both worker threads, ending program.\n";

    return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM