kostaleonard / leocoin

Leo's cryptocurrency
MIT License
4 stars 1 forks source link

Consider adding helper functions to do pthread_cond_timedwait for fields in mine_blocks_args_t #34

Open kostaleonard opened 1 month ago

kostaleonard commented 1 month ago

As a miner, I want to have helper functions that properly wait on condition variables in mine_blocks_args_t so that I don't have to repeat boiler plate and error prone multithreaded code.

Here's one example of the timed wait code.

    *args.should_stop = true;
    clock_gettime(CLOCK_REALTIME, &ts);
    // One second timeout.
    // TODO this timeout code should probably be in a helper function
    ts.tv_sec += 1;
    pthread_mutex_lock(&args.exit_ready_mutex);
    while (!*args.exit_ready) {
        int result = pthread_cond_timedwait(
            &args.exit_ready_cond, &args.exit_ready_mutex, &ts);
        if (ETIMEDOUT == result) {
            assert_true(false);
        }
    }
    pthread_mutex_unlock(&args.exit_ready_mutex);