6

I have in a Server object multiple thread who are doing the same task. Those threads are init with a Server::* routine.

In this routine there is a infinite loop with some treatments.

I was wondering if it was thread safe to use the same method for multiple threads ? No wonder for the fields of the class, If I want to read or write it I will use a mutex. But what about the routine itself ?

Since a function is an address, those thread will be running in the same memory zone ?

Do I need to create a method with same code for every thread ?

Ps: I use std::mutex(&Server::Task, this)

Thibaud Auzou
  • 129
  • 10
  • 1
    As long as your mutex protects against _all_ forms of potentially corrupted data, you should be fine. – Tim Biegeleisen Jul 15 '15 at 00:03
  • Yeah but this is my question: In the case of a member function aren't every instruction a possibility of corrupted data ? Will my "while" instruction be corrupted by multiple thread executing it in the same time ? Since this method has a unique address... – Thibaud Auzou Jul 15 '15 at 00:09
  • 1
    It's very unclear what you're asking. Please provide a concrete, complete and small example. – Cheers and hth. - Alf Jul 15 '15 at 00:11
  • 1
    You seem to confuse *code* and *data*. – Kerrek SB Jul 15 '15 at 00:36
  • a member function is just a function that takes an implicit `this` parameter. Nothing special about it beyond that. – xaxxon Jul 12 '22 at 17:01

4 Answers4

5

There is no problem with two threads running the same function at the same time (whether it's a member function or not).

In terms of instructions, it's similar to if you had two threads reading the same field at the same time - that's fine, they both get the same value. It's when you have one writing and one reading, or two writing, that you can start to have race conditions.

user253751
  • 57,427
  • 7
  • 48
  • 90
  • Thank you. I knew for classic C function but I had strong doubts with member function since those belong to an instance of an object. Therefore I tend to see member function as a field or a data which would need to be mutexed. – Thibaud Auzou Jul 15 '15 at 00:18
  • @ThibaudAuzou: Do you understand **why** shared data needs to be protected by a mutex? – MSalters Jul 15 '15 at 09:46
4

In C++ every thread is allocated its own call stack. This means that all local variables which exist only in the scope of a given thread's call stack belong to that thread alone. However, in the case of shared data or resources, such as a global data structure or a database, it is possible for different threads to access these at the same time. One solution to this synchronization problem is to use std::mutex, which you are already doing.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
1

While the function itself might be the same address in memory in terms of its place in the table you aren't writing to it from multiple locations, the function itself is immutable and local variables scoped inside that function will be stacked per thread.

If your writes are protected and the fetches don't pull stale data you're as safe as you could possibly need on most architectures and implementations out there.

0

Behind the scenes, int Server::Task(std::string arg) is very similar to int Server__Task(Server* this, std::string arg). Just like multiple threads can execute the same function, multiple threads can also execute the same member function - even with the same arguments.

A mutex ensures that no conflicting changes are made, and that each thread sees every prior change. But since code does not chance, you don't need a mutex for it, just like you don't need a mutex for string literals.

MSalters
  • 173,980
  • 10
  • 155
  • 350