Riverbed Interview Question
Software Engineer / Developers@Hard: Give a concrete answer or shut up!
@anonymous: If life is short, stop giving stupid replies
========================
Spin lock is hardly even better than mutex, since its mostly implemented as a while loop with condition check. Now two processes or threads can check the same condition together and enter, which will be a race condition. Obviously lot of waste of CPU cycles.
Mutex however puts a process which was unable to acquire a lock put to a queue and wake one up upon unlocking.
Spinlocks are efficient if threads are only likely to be blocked for a short period of time, as they avoid overhead from operating system process re-scheduling or context switching. For this reason, spinlocks are often used inside operating system kernels. However, spinlocks become wasteful if held for longer durations, both preventing other threads from running and requiring re-scheduling.
Thread is independ flow within a process and it is part of process. each process has any number of threads can create/run. But thread should share resources from the process.
spinlocks : A spinlock is a lock where the thread simply waits in a loop repeatedly checking until the lock becomes available.
spinlock is better than mutex because thread will be waiting in loop ntil the lock becomes available
Spin locks are better only when you want that process to acquire the critical section immediately. But most of the times they are not good because they waste lot of CPU cycles. Also spin locks should be used only when the underline hardware has multiple cores otherwise you run into deadlock.
@shahsi..wht a pathetic answer.
- Hard November 10, 2010