首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >互斥计数字符在使用线程的文件中的出现情况

互斥计数字符在使用线程的文件中的出现情况
EN

Stack Overflow用户
提问于 2017-10-19 15:28:35
回答 1查看 452关注 0票数 0

嗨,我正在写一个代码,用于计算一个或多个文件中使用线程的某个字母出现的次数。我必须对每个文件使用一个线程,使用互斥锁来修改全局总数。

这是我的密码:

代码语言:javascript
复制
typedef struct _CharFile{

    char c;
    char *fileName;

} CharFile;

pthread_mutex_t count = PTHREAD_MUTEX_INITIALIZER;
int sum = 0;

void *CountFile(void *threadarg);

int main(int argc, const char * argv[]) {

    pthread_t threads[argc-2];
    int chck, t;
    CharFile cf;

    if (argc <= 2){

        perror("Wrong inputs: need to select a letter and one or more files\n");
        exit(EXIT_FAILURE);

    } else if (argc > 51) {

        perror("Too many files\n");
        exit(EXIT_FAILURE);

    }

    for ( t=0 ; t<argc-2 ; t++ ){

        cf.c = argv[1][0];
        cf.fileName = (char *)argv[t + 2];

        chck = pthread_create(&threads[t], NULL, CountFile, (void *) &cf);
        if (chck){
            printf("ERROR; return code from pthread_create() is %d\n", chck);
            exit(EXIT_FAILURE);
        }

    }

    for (int i = 0; i < 2; i++)
        pthread_join(threads[i], NULL);

    printf("%lld occurrences of the letter %c in %lld threads\n", (long long)sum, argv[1][0], (long long)argc-2);

    return 0;
}

void *CountFile(void *threadarg){

    FILE *in;
    CharFile *cf;
    char c;
    int counter = 0;

    cf = (CharFile *) threadarg;
    in = fopen(cf->fileName, "r");

    if (in == NULL){

        perror("Error opening the file!\n");
        pthread_exit(NULL);

    }

    while (fscanf(in, "%c", &c) != EOF){

        if(c == cf->c){

            counter ++;

        }

    }

    fclose(in);

    pthread_mutex_lock(&count);
    sum += counter;
    printf("%d sum value, %d counter value \n", sum, counter);
    pthread_mutex_unlock(&count);

    pthread_exit(NULL);
}

我不知道哪里出了问题,但是用一些我有很多字母的文件,结果是很奇怪的。

运行这一行./a.out f file1.in lorem.txt notes.txt两次,我得到以下结果:

代码语言:javascript
复制
4 sum value, 4 counter value 
9 sum value, 5 counter value 
13 sum value, 4 counter value 
13 occurrences of the letter f in 3 threads

4 sum value, 4 counter value 
8 sum value, 4 counter value 
12 sum value, 4 counter value 
12 occurrences of the letter f in 3 threads

如果我只使用一个文件运行一个简单的命令行,则计数是正确的,即./a.out f file1.in

代码语言:javascript
复制
5 sum value, 5 counter value 
5 occurrences of the letter f in 1 threads

谢谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-19 15:42:58

我认为你的问题可能在这里:

代码语言:javascript
复制
for ( t=0 ; t<argc-2 ; t++ ){

    cf.c = argv[1][0];
    cf.fileName = (char *)argv[t + 2];

    chck = pthread_create(&threads[t], NULL, CountFile, (void *) &cf);
    if (chck){
        printf("ERROR; return code from pthread_create() is %d\n", chck);
        exit(EXIT_FAILURE);
    }

}

将指向同一变量(即cf)的指针传递给所有线程。将指针传递到第一个线程后,您将更改cf的值,并将指向它的指针传递到第二个线程,诸如此类。

因此,主循环可能会在“刚开始”线程读取其值之前更改cf,即线程可能会打开另一个文件。

确保每个线程都有自己的cf,方法是将cf更改为数组,即每个线程一个元素。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46833529

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档