C语言 全局计数器和并发线程,如何检查计数器进度?

6yoyoihd  于 2023-02-18  发布在  其他
关注(0)|答案(1)|浏览(150)

我试着写一个简单的程序来理解线程。我希望每个线程将一个全局变量“counter”递增到400万。每个线程只计数到200万。我在每个函数的末尾放置了一个print语句,以查看函数完成时有多少次迭代以及全局计数器在哪里。但是thread 1Func中的全局计数器总是非常高。比如380 - 390万,然后在thread 2Func中计数器总是4 mil(如预期)。
我做的对吗?thread 1func总是打印这么高的“counter”值有什么原因吗?我想它应该在2 mil - 4 mil之间更均匀。任何建议都将非常感谢!

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>

#define MAX 2000000UL
pthread_mutex_t lock;
//pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;

struct shared_data
{
    int value;     /* shared variable to store result*/
    
};

struct shared_data  *counter;

void* thread1Func(void * tid){
    uint32_t i = 0;
    while(i < MAX){
        
        if(pthread_mutex_trylock(&lock) == 0){
            counter->value++;
            pthread_mutex_unlock(&lock);
            i++;
        }
    }
    printf("I am thread 1. I counted %d times. Global counter = %d\n", i, counter->value);
    return NULL;
}

void* thread2Func(void * tid){
    
    uint32_t i = 0;
    while(i < MAX){
        
        if(pthread_mutex_trylock(&lock) == 0){
            counter->value++;
            pthread_mutex_unlock(&lock);
            i++;
        }

    }
    printf("I am thread 2. I counted %d times. Global counter = %d\n", i, counter->value);
    return NULL;
}

int main() {
    
    counter = (struct shared_data *) malloc(sizeof(struct shared_data));
    printf("Initial Counter Value: %d\n", counter->value);
    
    pthread_t thread1;
    pthread_t thread2;
    pthread_mutex_init(&lock, NULL);
    
    pthread_create(&thread1, NULL, thread1Func, NULL);
    pthread_create(&thread2, NULL, thread2Func, NULL);
    
    
    pthread_join(thread1,NULL);
    pthread_join(thread2,NULL);
    
    
    printf("Final Counter: %d\n", counter->value);
    return 0;
}
4ioopgfo

4ioopgfo1#

有趣的例子,干得好!
尝试更改线程优先级,看看会发生什么,从而看到不同的计数(请参阅下面的代码)。
也许可以考虑添加一个信号量,在两个线程之间乒乓计数,这样它们就可以平等地执行,并报告3999999和4000000作为最终计数。
我相信你也有其他的想法,谢谢你的帖子。

gcc main.c -o main
./main
Initial Counter Value: 0
I am thread 2. I counted 2000000 times. Global counter = 3880728
I am thread 1. I counted 2000000 times. Global counter = 4000000
Final Counter: 4000000
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>

#define MAX 2000000UL
pthread_mutex_t lock;
//pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;

struct shared_data
{
    int value;     /* shared variable to store result*/
    
};

struct shared_data  *counter;

void* thread1Func(void * tid){
    uint32_t i = 0;
    while(i < MAX){
        
        if(pthread_mutex_trylock(&lock) == 0){
            counter->value++;
            pthread_mutex_unlock(&lock);
            i++;
        }
    }
    printf("I am thread 1. I counted %d times. Global counter = %d\n", i, counter->value);
    return NULL;
}

void* thread2Func(void * tid){
    
    uint32_t i = 0;
    while(i < MAX){
        
        if(pthread_mutex_trylock(&lock) == 0){
            counter->value++;
            pthread_mutex_unlock(&lock);
            i++;
        }

    }
    printf("I am thread 2. I counted %d times. Global counter = %d\n", i, counter->value);
    return NULL;
}

int main() {
    
    counter = (struct shared_data *) malloc(sizeof(struct shared_data));
    printf("Initial Counter Value: %d\n", counter->value);
    
    pthread_t thread1;
    pthread_t thread2;
    pthread_mutex_init(&lock, NULL);

    pthread_attr_t attr;
    struct sched_param sch_params;
    pthread_attr_init(&attr);
    pthread_attr_getschedparam(&attr, &sch_params);
    sch_params.sched_priority = 99;
    //pthread_attr_setschedpolicy(&attr, SCHED_RR);
    pthread_attr_setschedparam(&attr, &sch_params);
    pthread_create(&thread1, &attr, thread1Func, NULL);

    sch_params.sched_priority = 1;
    pthread_create(&thread2, &attr, thread2Func, NULL);
    
    
    pthread_join(thread1,NULL);
    pthread_join(thread2,NULL);
    
    
    printf("Final Counter: %d\n", counter->value);
    return 0;
}

相关问题