题目:实现两个线程交替计算1-10000内的合数 or 素数

// method 1

#include <thread>
#include <cmath>
#include <atomic>

#define Max 10000

typedef std::function<uint32_t()> BeginHook;
typedef std::function<void(uint32_t, bool)> EndHook;

std::atomic<char> select_thr{'N'};

uint32_t GetNumber() {
    static uint32_t num = 1;
    return num++;
}

void Stat(uint32_t num, bool is_su_shu) {

    if (num <= Max)
        printf("thr-%c %#x: %u is %s\n",
               select_thr.load(), std::this_thread::get_id(), num, is_su_shu ? "su shu" : "he shu");

    select_thr = num % 2 ? 'a' : 'b';
}

void ThreadCalcNumber(BeginHook get_num, EndHook sched, char my_name) {

    while (true) {

        while (select_thr != my_name) {
            std::this_thread::yield();
        }

        auto num = get_num();

        if (num > Max) {
            sched(num, 0);
            break;
        }

        if (1 == num || 2 == num) {
            sched(num, true);
            continue;
        }

        uint8_t counts = 0;
        for (int i = 2; i <= pow(static_cast<double>(num), 0.5); ++i) {
            if (num % i == 0) {
                counts++;
            }

            if (counts) {
                break;
            }
        }

        sched(num, counts == 0);
    }

}

int main() {
    std::thread thr_a(ThreadCalcNumber, GetNumber, Stat, 'a');
    std::thread thr_b(ThreadCalcNumber, GetNumber, Stat, 'b');

    select_thr = 'b';

    if (thr_a.joinable())
        thr_a.join();

    if (thr_b.joinable())
        thr_b.join();

    printf("!!!stop!!!\n");
}

// method 2

#include <thread>
#include <cmath>
#include <atomic>

#include <condition_variable>
#include <mutex>

#define Max 10000

typedef std::function<uint32_t()> BeginHook;
typedef std::function<void(uint32_t, bool)> EndHook;

char select_thr{'N'};

std::mutex g_mtx;
std::condition_variable cv;

uint32_t GetNumber() {
    static uint32_t num = 1;
    return num++;
}

void Stat(uint32_t num, bool is_su_shu) {

    if (num <= Max)
        printf("thr-%c %#x: %u is %s\n",
               select_thr, std::this_thread::get_id(), num, is_su_shu ? "su shu" : "he shu");

    select_thr = num % 2 ? 'a' : 'b';
    cv.notify_all();
}

void ThreadCalcNumber(BeginHook get_num, EndHook sched, char my_name) {

    while (true) {

        std::unique_lock<std::mutex> Guard(g_mtx);
        cv.wait(Guard, [my_name] { return select_thr == my_name; });
//        while (select_thr != my_name) {
//            cv.wait(Guard);
//        }

        auto num = get_num();

        if (num > Max) {
            sched(num, 0);
            break;
        }

        if (1 == num || 2 == num) {
            sched(num, true);
            continue;
        }

        uint8_t counts = 0;
        for (int i = 3; i <= pow(static_cast<double>(num), 0.5); ++i) {
            if (num % i == 0) {
                counts++;
            }

            if (counts) {
                break;
            }
        }

        sched(num, counts == 0);
    }

}

int main() {
    std::thread thr_a(ThreadCalcNumber, GetNumber, Stat, 'a');
    std::thread thr_b(ThreadCalcNumber, GetNumber, Stat, 'b');

    select_thr = 'b';

    if (thr_a.joinable())
        thr_a.join();

    if (thr_b.joinable())
        thr_b.join();

    printf("!!!stop!!!\n");
}


备份地址: 【实现两个线程交替计算合数 or 素数