Yashwant Rao
Yashwant Rao

Reputation: 11

Sequence of tasks in Multithreading

I am new to threads and multithreading.(Win socket API) I am trying to achieve the sequence of tasks in 2 threads with proper synchronization.

no. of threads 2 : Thread_A and Thread_B

Thread_A{
    
    print task_A
    
    print task_D
}

Thread_B{
    
    print task_B
    
    print task_C
}

The expected output should be print task_A , print task_B, print task_C, print task_D.

do i need to use 2 semaphores or 1 can be use ??

Thread_A{

while(1){

    wait semaphore

           print task_A

    release semaphore
        
    wait semaphore

           print task_D

    release semaphore
}

}

Thread_B{

while(1){

       wait semaphore

           print task_B
    
           print task_C

      release semaphore

}

}

for once it goes A,B,C,D but second time B,C, A,B

Upvotes: 1

Views: 59

Answers (2)

Yashwant Rao
Yashwant Rao

Reputation: 11

I have tried with condition variables to ensure that task 1 is performed before task 2 and also for task 3 and 4. ref : Windows control variable Inital value of done and flag is 0

Thread 1

  while(1){
    
            EnterCriticalSection (&Tx_data);
            while (done == 1 )
            {
                SleepConditionVariableCS (&Status, &Tx_data, INFINITE);
            }
                printf("\n  1  "); // task A

                done =1;
                flag =0;
            LeaveCriticalSection (&Tx_data);
            WakeConditionVariable (&Status);


            EnterCriticalSection (&Tx_data);
            while (flag == 0 )
            {
                SleepConditionVariableCS (&flags, &Tx_data, INFINITE);
            }
                printf(" 4  "); // task D

            LeaveCriticalSection (&Tx_data);
            WakeConditionVariable (&Status);
}

Thread 2

     while(1){
    
            EnterCriticalSection (&Tx_data);
            while (done == 0 )
            {
                SleepConditionVariableCS (&Status, &Tx_data, INFINITE);
            }
                printf("  2  ");// task B
                printf("  3  ");// task C
                done =0;
                flag =1;
                
             LeaveCriticalSection (&Tx_data);
             WakeConditionVariable (&flags);
             WakeConditionVariable (&Status);
    }

The result ok and look like

1 2 3 4

1 2 3 4

is this approach with the use of condition variable to sequentially operate 1 2 3 4 tasks correct ?. It works for few tests, but is it reliable and fail proof ?

Upvotes: 0

Solomon Slow
Solomon Slow

Reputation: 27115

When your program starts those two tasks, what guarantees that Thread_A will acquire the semaphore before Thread_B acquires it? Certainly, there's nothing in your program (at least, nothing in the part of your program that you've showed) that guarantees it.

And, when Thread_A releases the semaphore and then immediately tries to acquire it again, what guarantees that Thread_B will get it first? Again, there's nothing in your program that guarantees it.

Your example uses the semaphore like a mutex, and you can't achieve that kind of sequencing with a mutex.

do i need to use 2 semaphores...?

That's what I would do. I'd have, for example, task_A release task_B's semaphore to signal that it's task_B's turn to run, and then task_A would acquire its own semaphore to wait for its own turn again.


But NOTE!

When you use two or more threads in this way, where each thread waits its turn, and no two threads are ever doing any interesting things at the same time, then that defeats the entire purpose of multi-threading. The only reason to ever create a thread is so that it can do something* at the same time as other threads in the program are doing their things.

*Even if it's only just waiting for input from the user or waiting for a message from a remote network host, that's still a "thing" that the thread can be doing at the same time as other threads doing their things.

Upvotes: 2

Related Questions