ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
This question is a little vague since the particular implementation of these various objects might vary a lot by language - if you can give a better idea of what context you want to use them in, it might help.
Doing my best to answer your question though, a sempaphore is generally just a number which can be incremented or decremented via an atomic operation (that is an operation which cannot be interrupted by another process/thread). A thread wanting to access some limited resource will check if the value of the semaphore is positive, and if it is, will decrement the semaphore, and then access the resource - on completion the thread will increment the value of the semaphore. So the basic implementation of a semaphore has a very low overhead. A lock or a mutex is just a binary semaphore (or a semaphore with a starting value of 1).
The main differences between the various kinds of semaphores is in what a thread is expected to do if the semaphore's count is non-positive. In a spinlock, the thread simply continues to execute in a loop 'spinning,' until the resource becomes available. This allows rapid execution of the blocked operation, but can tie up system resources running a useless loop if the resource doesn't become available quickly. In most ordinary semaphores and locks the thread will reschedule itself to check for the resource's availability. This lets other threads run, but does involve the overhead of rescheduling. One other common approach is to have a mutex which cues up commands to be executed. This avoids both spinning and rescheduling if your threads don't need to wait for the results of the commands, but just need to get the commands executed eventually.
Generally, the spinlock model is good if you never expect a thread to have to wait long to acquire the resource since it prevents the thread from being interrupted. If the resources is often going to be in use, though, or could be in use for long periods of time a spinlock becomes incredibly inefficient, and an ordinary semaphore is preferable. If your threads don't need access to the results of the operations on the resource, then some sort of queue mutex might be best. This will generally involve some sort of list structure (a linked list or an array maybe? This will depend on the particular implementation), but the overhead of creating this list structure is likely to be small compared to 'spinning' or rescheduling.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.