开发者

How do I make a function thread safe in C++? [closed]

开发者 https://www.devze.com 2023-03-14 07:00 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

Let's say I have a thread pool that has 5 child threads. And they are calling a function called "functionA()" How do I make the function to be thread safe?

Also 开发者_运维百科if those 5 threads are called at the same time then are they executed concurrently? or do they wait until a thread that currently works in the function to be finished ?

Thanks in advance..


A function is already thread safe if it doesn't modify non-local memory and it doesn't call any function that does. In this (trivial) case, you don't have to do anything.

You really want to think about protecting data, not functions. For example, let's say that the function modifies non-local data structure X. Provide a mutex to protect X and lock it before each access and unlock it after. You may have more than one function that accesses X (e.g. insertX(), deleteX(), ...). As long as you protect the data you'll be OK.


Using a mutex you can do that.

either:

mutex_lock(&mutex);
functionA();
mutex_unlock(&mutex);

or inside functionA();

int functionA() {
mutex_lock(&mutex);
// code
mutex_unlock(&mutex);
}

careful with the second solution, because if the function has other exit paths (a return in the middle for example) and the mutex in not unlocked, you have a situation called deadlock.


You need to make sure that the function runs identically everytime it is called.

This generally means that you need to protect any data that is accessed/written in the function.

For stand-alone free functions that generally means removing static elements from function.

If you are referring to a member function of some class, then you need to be more careful - because the class will generally store data independantely of the member function which may change between calls to that member. You will generally do this with a mutex.

For example:

//Threadsafe because the function do not change each time it is called
double add_one(double d)
{
  return d+1;
}

struct A
{
  double m_data;

  //Not thread safe because if function called simultaneously then m_data could change more than once before it is returned
  double 
  run(double d)
  {
    m_data += d;
    return m_data/2.0;
  }
}

This can be fixed by adding a mutex that demands that two threads cannot run the run member simultaneously. See Boost.Thread for a good mutex example.

struct A
{
  double m_data;
  mutex m_mutex;

  //Thread safe because mutex protects m_data from being written by multiple threads at once
  double 
  run(double d)
  {
    lock(m_mutex);  //lock the function, unlock on destruction.
    m_data += d;
    return m_data/2.0;
  }
}


One way to do this is to use a mutex to ensure that only a single thread is executing in the function at any one time. Of course, this presumes that this is the type of thread safety you are referring to. Being thread-safe could mean many different things. For example, take a read of Eric Lipperts post titled What is this thing you call "thread safe"?

If those 5 threads are called at the same time then are they executed concurrently or do they wait until a thread that currently works in the function to be finished?

Unless you impose some synchronisation mechanism (e.g. a mutex) then the threads execute the function concurrently. Whether or not this is a problem for your function, and what the solution is, depends on the data access patterns of that function.


It depends on what you want to do/ what you understand under threadsafe. Normally you dont want threads to access the same data concurrently so you serialize this. But this means if one thread is in the function all others have to wait. This is called a mutex(mutual exclusion). The other way would be to have multiple threads in the function but a limited number. There you could apply a semaphore.

So what do you want to do?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号