linux kernel - thread execution : how to ensure the systematic starting of thread -
i seeing different ways of starting threads in ubuntu , in other linux platform.
pthread_create ( &thread1, null, (void *) &myfun1, (void *) msg1); pthread_create ( &thread2, null, (void *) &myfun2, (void *) msg2); pthread_create ( &thread3, null, (void *) &myfun3, (void *) msg3); pthread_create ( &thread4, null, (void *) &myfun4, (void *) msg4);
in above case of ubuntu , first thread4 starting while in case of other linux os it's thread1. when checked reason, seems because of scheduling policies(correct me if wrong).
in these cases how ensure first thread (thread1) executes first despite of different linux flavours.
/generic query/, scheduling policy not depend upon kernel ? because 2 different types of thread execution seen in different linux flavours.
pseudo code makes thread 1 start before other thread execute tasks:
mutex mutex = ... /* static init here */ condition cond = ... /* static init here */ boolean first_thread_running = false; thread1 { mutex_lock first_thread_running = true; cond_signal mutex_unlock /* things */ } thread<n> /* <n> = {2, 3, 4} */ { mutex_lock while (not first_thread_running) cond_wait mutex_unlock /* thread1 definitly runs */ /* things */ } main { /* order of following statements not matter, thread 1 started before other threads "do things". */ thread4_create thread2_create thread1_create thread3_create ...
Comments
Post a Comment