Qualcomm Interview Question
Software Engineer / Developerst1.start();
t2.start();
In run method check whats the running thread if its not the one which you wanted to run first(Lets say t1).Put t2.join stmt meaning you can only run the remaining block once t2 will end the critical section.
Or just put a semaphore check.
Please correct me If i am wrong.
It seems like he is asking you to design a barrier. Here's one solution.
Initialize a semaphore called barrier with value 0. Also, have a variable num_threads, which is a count of the number of threads to be waited at the barrier. So, when a thread reaches the particular synchronization/barrier point in the code it does:
num_threads++;
if(num_threads == 2) // 2 in this case
{
for(i=0;i<2; i++)
V(barrier); // wakes up all threads blocked on the semaphore barrier..
// in this case 2
}
else
P(barrier); // block thread at this point if all threads have not reached
Well that should work if you wrap num_threads in a mutex; but otherwise you can get your threads incrementing it at the same time, and writing back a value of 1.
Also, you give too many wakeups. You'd want to give num_threads - 1 wakeups (V(barrier)s), because the last thread doesn't need to wait. For that same reason it also doesn't need to increment num_threads to know whether it's the last thread (unless there are other threads coming along behind it, ie you're releasing 'batches' of N threads at a time).
How abt making all threads block on a condition variables and increment a counter. If the counter is equal to #threads spawned, notify all threads. This way the last thread increments the counter and notifies all threads. Till then they will be blocked.
This isn't safe. One of the threads might have seen and incremented the count, decided it has to wait, but gets suspended by the OS before it gets to wait on the condition. The 'last' thread comes through and broadcasts the wakeup to everyone on the condition. The 'slow' thread then finally gets there, but the signal has missed it - signals apply only to threads that are already there. So it waits forever. You have to use a semaphore, which will safely, atomically etc, keep a count.
we can use semaphore to achieve such synchronization
initialize the semaphore value to zero
Process 1
do operations,
//sync point
signal semaphore
Process 2
do operations
wait for semaphore
If t2 is not waiting t2.signal is lost?
Alterante solution. Start a semaphore in taken state. When the common function enters, wait on the semaphore. When 2nd thread reaches the function give the semaphore so that previous thread wakesup and continous.
int threadCount=0;
Semaphore barrierSem(0);
Mutex mu;
void barrier()
{
Mutex.lock( );
threadcount++;
Mutex.unlock( );
if(threadcount ==2)
{
barrieSem.post( );
}else if (threadcount ==1)
{
barrierSem.wait(); //1st entered thread waits here
}
}
Thread1
{
//some code
barrier();
}
Thread2
{
//some code;
barrier();
}
If t2 is not waiting t2.signal is lost?
Alterante solution. Start a semaphore in taken state. When the common function enters, wait on the semaphore. When 2nd thread reaches the function give the semaphore so that previous thread wakesup and continous.
int threadCount=0;
Semaphore barrierSem(0);
Mutex mu;
void barrier()
{
Mutex.lock( );
threadcount++;
Mutex.unlock( );
if(threadcount ==2)
{
barrieSem.post( );
}else if (threadcount ==1)
{
barrierSem.wait(); //1st entered thread waits here
}
}
Thread1
{
//some code
barrier();
}
Thread2
{
//some code;
barrier();
}
If t2 is not waiting t2.signal is lost?
Alterante solution. Start a semaphore in taken state. When the common function enters, wait on the semaphore. When 2nd thread reaches the function give the semaphore so that previous thread wakesup and continous.
int threadCount=0;
Semaphore barrierSem(0);
Mutex mu;
void barrier()
{
Mutex.lock( );
threadcount++;
Mutex.unlock( );
if(threadcount ==2)
{
barrieSem.post( );
}else if (threadcount ==1)
{
barrierSem.wait(); //1st entered thread waits here
}
}
Thread1
{
//some code
barrier();
}
Thread2
{
//some code;
barrier();
}
- Anonymous February 28, 2011