Archive

Archive for May, 2013

Mutex vs. binary semaphore

28.05.2013 8 comments

Mutices and semaphores are among the most basic tools in multithreaded programming. However most people I asked do not know what is the difference between them. So please let me introduce you to them.

Consider a resource which is shared between multiple threads. For example a container. You don’t want to have multiple threads modifying the container simultaneously, or one thread modifying the container while other threads are reading from it, otherwise you will end up with classical race conditions and unpredictable things will happen.

To guard a resource from other threads while you are accessing it, you use a synchronization primitive, which you conceptually associate with the guarded resource. Threads can lock the synchronization primitive when they need to access the resource. Then they can release the primitive after they are finished with accessing the resource. When the synchronization primitive is already locked, a thread trying to lock it will wait/stall until the other thread who locked it – unlocks it.

At the first glance, both mutex and binary semaphore fit the description of the above synchronization primitive. Well, not quite. Using binary semaphore in place of a mutex is a bad idea.

Conceptually a semaphore is like an integer. You can increment it and you can decrement it. If the semaphore’s value is 0, the thread trying to decrement it will wait/stall until somebody else increments it. This way, the semaphore never has a negative value.

A binary semaphore is just a semaphore capped at one, i.e. it’s value cannot exceed one. You can treat the decrement operation as “lock” and the increment operation as “unlock”.

The problem with the semaphore is that any thread can increment it or decrement it. In particular, if the semaphore’s value is 0 (“locked”), another thread can increment it (“unlock”), even if this is not the thread which locked it! It takes more discipline to write code which correctly uses binary semaphores for locking and there is still a potential for error.

Another problem is that most semaphore implementations allow sharing semaphores between processes. This makes them much heavier than e.g. POSIX mutices or critical sections on Windows, which are lightweight, because they only work within one process and don’t require calling into kernel space in most cases.

Unlike binary semaphores,mutices may also have another interesting property, depending on an implementation, i.e. they can be recursive. A recursive mutex can be locked twice from the same thread. This allows you to write an accessor function and not have to care whether the mutex is already locked by the current thread or not. However it is generally not recommended to use recursive mutices, the necessity for recursive mutices is a sign of a bad design and indicates that there may be potential problems with the interfaces or even hidden multithreading bugs.

In general mutices (or critical sections on Windows) are typically recommended over binary semaphores as synchronization primitives between multiple threads in the same process.

Advertisements
Categories: Computing

Unenforceable law

19.05.2013 3 comments

A few years ago California introduced a law which bans texting or talking over the phone while driving. The problem is, that this law is unenforceable.

Every day I see many people clicking on their smartphones when waiting on the red light and often even when driving. Not only they risk their and other drivers’ lives, but they also show disrespect to other people. It always makes me angry when I am waiting on the red light behind somebody, the red light turns in to green and I am waiting multiple seconds before the clicker in front of me notices the green light. Often I have to honk to draw that person’s attention, the person starts driving lazily and keeps clicking.

The problem is so widespread that the police has no way of catching the clickers and enforcing the law. Maybe in 10 years we will have technology which blocks the phone when it is inside a car, maybe phones will automatically detect that they are inside the car and will lock their UI. Also self-driving cars will probably also take over when they detect that the driver is not paying attention or is drunk. Until then, the anti-texting law should probably be repealed, for it makes no sense.

What do you think? Do clickers annoy you when they are blocking your driving, do they offend you, or are you OK waiting for someone whose mind is somewhere else and not on the road?

Categories: Other

Skyline (2010)

7.05.2013 Leave a comment

I’ve seen this movie a long time ago, but it was in some way exceptional and thus memorable. I highly recommend seeing it if you’re into the SciFi genre.

It is not a AAA movie, I did not know the actors earlier and some of their acting was not of the best, although it was not totally bad either.

The visual effects are interesting, the story is a little bit believable and the aliens in this movie are awesome in my opinion. You can’t tell the same about most SciFi movies, especially when you take into account the recent outbreak of idiotic superhero movies. Skyline brings some fresh ideas and an original plot and that’s why I think it’s worth seeing.

Spoilers

The trailer makes it clear that it’s a movie about an invasion. What I like is that it is not one of those movies in which humans always win. To the contrary, puny humans stand no chance! There is a little twist at the end though.

The scene with huge aliens wandering around the city is my favorite.

There are some things I did not like, of course. For example the whole setting is unrealistic – the aliens came to conquer humans just to use their brains in their machines. It makes no sense to me, because computers and artificial brains we will create in the coming decades can have much better capabilities and performance than human brain. Aliens capable of interstellar travel should have computer technology beyond what we can currently imagine.

Nevertheless, it’s worth seeing in my opinion, even as a pure entertainment. Enjoy!

Categories: Movie Reviews