帮我优化一下代码

#include <stdio.h>
#include <windows.h>

#define BUFFER_SIZE 5
#define NUM_PRODUCERS 2
#define NUM_CONSUMERS 2

HANDLE emptySlots;
HANDLE fullSlots;
HANDLE mutex;

int buffer[BUFFER_SIZE];
int in = 0;
int out = 0;

void produce(int data) {
    buffer[in] = data;
    in = (in + 1) % BUFFER_SIZE;
}

int consume() {
    int data = buffer[out];
    out = (out + 1) % BUFFER_SIZE;
    return data;
}

void SetCursorPosition(int x, int y) {
    COORD coord;
    coord.X = x;
    coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

DWORD WINAPI producerThread(LPVOID lpParam) {
    int producerId = *(int*)lpParam;
    int producedCount = 0;

    while (1) {
#include <stdio.h>
#include <windows.h>

#define BUFFER_SIZE 5
#define NUM_PRODUCERS 2
#define NUM_CONSUMERS 2

        HANDLE emptySlots;  // 空槽位信号量
        HANDLE fullSlots;   // 已占用槽位信号量
        HANDLE mutex;       // 互斥锁

        int buffer[BUFFER_SIZE];
        int in = 0;         // 生产者写入位置
        int out = 0;        // 消费者读取位置

        // 生产者生产资源
        void produce(int data) {
            buffer[in] = data;
            in = (in + 1) % BUFFER_SIZE;
        }

        // 消费者消费资源
        int consume() {
            int data = buffer[out];
            out = (out + 1) % BUFFER_SIZE;
            return data;
        }

        // 设置控制台光标位置
        void SetCursorPosition(int x, int y) {
            COORD coord;
            coord.X = x;
            coord.Y = y;
            SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
        }

        // 生产者线程函数
        DWORD WINAPI producerThread(LPVOID lpParam) {
            int producerId = (int)lpParam;
            int producedCount = 0;

            while (1) {
                WaitForSingleObject(emptySlots, INFINITE); // 等待空槽位信号量
                WaitForSingleObject(mutex, INFINITE);     // 等待互斥锁

                int data = rand() % 100 + 1;             // 产生随机的生产数据
                produce(data);                          // 将数据写入缓冲区
                SetCursorPosition(producedCount % BUFFER_SIZE * 2, BUFFER_SIZE + 3); // 设置光标位置，用于显示生产者生产的资源
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_INTENSITY); // 设置颜色
                printf("■ ");                            // 显示生产者生产的资源
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY); // 恢复颜色

                ReleaseMutex(mutex);                    // 释放互斥锁
                ReleaseSemaphore(fullSlots, 1, NULL);   // 发送已占用槽位信号，通知消费者

                producedCount++;                        // 增加生产数量

                // 显示生产数量，每5个资源显示一个计数器
                if (producedCount % 5 == 0) {
                    SetCursorPosition(producedCount / 5 - 1, BUFFER_SIZE + 4);
                    printf(“■”);
                }

                Sleep(rand() % 1000);                   // 随机休眠一段时间，模拟生产过程
            }

            return producedCount;
        }

        // 消费者线程函数
        DWORD WINAPI consumerThread(LPVOID lpParam) {
            int consumerId = (int)lpParam;
            int consumedCount = 0;

            while (1) {
                WaitForSingleObject(fullSlots, INFINITE);  // 等待已占用槽位信号
                WaitForSingleObject(mutex, INFINITE);      // 等待互斥锁

                int data = consume();                     // 从缓冲区读取数据
                SetCursorPosition(consumedCount % BUFFER_SIZE * 2, BUFFER_SIZE + 5); // 设置光标位置，用于显示消费者消费的资源
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_INTENSITY); // 设置颜色
                printf("■ ");                             // 显示消费者消费的资源
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY); // 恢复颜色

                ReleaseMutex(mutex);                     // 释放互斥锁
                ReleaseSemaphore(emptySlots, 1, NULL);   // 发送空槽位信号，通知生产者

                consumedCount++;                         // 增加消费数量

                Sleep(rand() % 1000);                    // 随机休眠一段时间，模拟消费过程
            }

            return consumedCount;
        }

        // 显示生产者、消费者资源数量
        void PrintResourcesCount(int producersCount, int consumersCount) {
            SetCursorPosition(20, BUFFER_SIZE + 3);
            printf("生产者生产资源    ", producersCount);

            SetCursorPosition(20, BUFFER_SIZE + 5);
            printf("消费者消费资源   ", consumersCount);
        }

        // 显示缓冲区状态
        void PrintBufferState() {
            SetCursorPosition(0, 0);
            printf("缓冲区内容: ");
            for (int i = 0; i < BUFFER_SIZE; i++) {
                if (i == out) {
                    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY); // 设置颜色
                }
                printf(“■ “);
                if (i == in) {
                    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY); // 恢复颜色
                }
            }
            printf(”\n”);

            SetCursorPosition(0, 1);
            printf(“空槽位数量: % d    \n”, BUFFER_SIZE - in + out);

            SetCursorPosition(0, 2);
            printf(“已占用槽位数量: % d    \n”, in - out);
        }

        int main() {
            // 创建信号量和互斥锁
            emptySlots = CreateSemaphore(NULL, BUFFER_SIZE, BUFFER_SIZE, NULL);
            fullSlots = CreateSemaphore(NULL, 0, BUFFER_SIZE, NULL);
            mutex = CreateMutex(NULL, FALSE, NULL);

            // 创建生产者线程
            DWORD producerThreadId[NUM_PRODUCERS];
            HANDLE producerThreads[NUM_PRODUCERS];
            int producerIds[NUM_PRODUCERS];
            for (int i = 0; i < NUM_PRODUCERS; i++) {
                producerIds[i] = i + 1;
                producerThreads[i] = CreateThread(NULL, 0, producerThread, &producerIds[i], 0, &producerThreadId[i]);
            }

            // 创建消费者线程
            DWORD consumerThreadId[NUM_CONSUMERS];
            HANDLE consumerThreads[NUM_CONSUMERS];
            int consumerIds[NUM_CONSUMERS];
            for (int i = 0; i < NUM_CONSUMERS; i++) {
                consumerIds[i] = i + 1;
                consumerThreads[i] = CreateThread(NULL, 0, consumerThread, &consumerIds[i], 0, &consumerThreadId[i]);
            }

            int producersCount = 0;
            int consumersCount = 0;

            // 循环显示缓冲区状态和生产者、消费者资源数量
            while (1) {
                PrintBufferState();
                PrintResourcesCount(producersCount, consumersCount);

                Sleep(1000);
            }

            // 等待线程结束，并释放资源
            WaitForMultipleObjects(NUM_PRODUCERS, producerThreads, TRUE, INFINITE);
            WaitForMultipleObjects(NUM_CONSUMERS, consumerThreads, TRUE, INFINITE);

            for (int i = 0; i < NUM_PRODUCERS; i++) {
                CloseHandle(producerThreads[i]);
            }
            for (int i = 0; i < NUM_CONSUMERS; i++) {
                CloseHandle(consumerThreads[i]);
            }
            CloseHandle(emptySlots);
            CloseHandle(fullSlots);
            CloseHandle(mutex);

            return 0;
        }