Exemple #1
0
void thread_func(void *arg)
{
    ABT_thread my_handle;
    ABT_thread_state my_state;
    ABT_thread_self(&my_handle);
    ABT_thread_get_state(my_handle, &my_state);
    if (my_state != ABT_THREAD_STATE_RUNNING) {
        fprintf(stderr, "ERROR: not in the RUNNUNG state\n");
    }
    ABT_thread_release(my_handle);

    thread_arg_t *t_arg = (thread_arg_t *)arg;
    ABT_thread next;

    ABT_test_printf(1, "[TH%d]: before yield\n", t_arg->id);
    next = pick_one(t_arg->threads, t_arg->num_threads);
    ABT_thread_yield_to(next);

    ABT_test_printf(1, "[TH%d]: doing something ...\n", t_arg->id);
    next = pick_one(t_arg->threads, t_arg->num_threads);
    ABT_thread_yield_to(next);

    ABT_test_printf(1, "[TH%d]: after yield\n", t_arg->id);

    ABT_task task;
    ABT_task_self(&task);
    if (task != ABT_TASK_NULL) {
        fprintf(stderr, "ERROR: should not be tasklet\n");
    }
}
ABT_thread pick_one(ABT_thread *threads, int num_threads, unsigned *seed)
{
    int i;
    ABT_thread next;
    ABT_thread_state state = ABT_THREAD_STATE_TERMINATED;
    while (state == ABT_THREAD_STATE_TERMINATED) {
        i = rand_r(seed) % num_threads;
        next = threads[i];
        if (next != ABT_THREAD_NULL) {
            ABT_thread_get_state(next, &state);
        }
    }
    return next;
}