Question

I came across this line "some functions are inherently thread-safe, for example memcpy()"

Wikipedia defines "thread-safe" as:

A piece of code is thread-safe if it only manipulates shared data structures in a manner that guarantees safe execution by multiple threads at the same time.

OK. But what does inherently mean? Is it related to inheritance?

Was it helpful?

Solution

It is not related to inheritance. It is an informal expression and means more like
"some functions are thread-safe by their nature". For example a function which does not
touch any shared values/state is thread safe anyway i.e. "is inherently thread-safe".

OTHER TIPS

In this context I interpret it as "without having been designed to achieve it, it still is thread-safe".

There is no direct link to the concept of inheritance, although of course the words are related. This is not an example of inheritance in the object-oriented programming sense, of course. This is just a function, that from its core nature gets the property of being thread-safe.

Of course there's nothing magic about memcpy() being inherently thread-safe, either. Any function without internal state or "shared" side-effects will be so, which is why functional programming, where all functions are supposed to be "pure" and lack side-effects, lends itself so well to parallel programming.

In practice it's hard on typical computers to get "real work" done without side-effects, particularly I/O is very much defined by its side-effects. So even pure functional languages often have some non-functional corners.

Update: Of course, memcpy() is not free from side-effects, it's core purpose is to manipulate memory which, if shared between threads, certainly isn't safe. The assumption has to be that as long as the destination areas are distinct, it doesn't matter if one or more threads run memcpy() in parallel.

Contrast this with e.g. printf(), which generates characters on a single (for the process) output stream. It has to be explicitly implemented (as required by POSIX) to be thread-safe, whereas memcpy() does not.

An inherently thread safe function, is safe without having to take any specific design decisions regarding threading, it is thread safe simply by virtue of the task it performs as opposed to being redesigned to force thread safety. Say I write the very simple function:

int cube(int x)
{
    return x*x*x;
}

It is inherently thread safe, as it has no way of reading from or writing to shared memory. However I could also make a function which is not thread safe but make it thread safe through specific design and synchronization. Say I have this similar function to before:

void cubeshare()
{
    static int x;
    x = x * x * x;
    printf("%l", x);
}

This is not thread safe, it is entirely possible it could have the value of x change between each use (well this is actually unlikely in reality as x would get cached but lets say we are not doing any optimization).

We however could make this thread safe like this (this is pseudo code, a real mutex is more complicated):

void cubesharesafe(mutex foo)
{
    static int x;
    lockmutex(foo);
    x = x * x * x;
    printf("%l", x);
    unlockmutex(foo);
}

This is however not inherently thread safe, we are forcing it to be through redesign. Real examples will often be far more complicated than this but I hope that this gives an idea taken to the most simple possible level. If you have any questions please comment bellow.

In case of memcpy, only a single thread is able to provide writes from a specific source to a specific destination. : thread-safe by initial design so.

Inherently means: without needing to "tune" the base function to achieve the goal, in this case: thread safety.

If multiple threads could interfere the same "channel" at the same time, you would end up with problem of thread-safety related to shared chucks of data.

inherent means Existing in something as a permanent. it has nothing to do with inheritance.. by default,or already some methods are thread safe...in order to protect or avoid multitasking problems.. vector,hash table..are some of the example classes that are inherently thread safe.. nothing..confusing..there are some functions..which is thread safe by default..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top