Is Thread member function safe?

I have a multi-threaded server thread that performs the same task. These threads are init using the Server :: * procedure.

In this procedure, there is an endless loop with some processing.

I was wondering if it is possible to use a stream safe in the same way for multiple threads? Not surprisingly, for class fields, if I want to read or write, I will use the mutex. But what about the procedure itself?

Since the function is an address, will these threads work in the same memory area?

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

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

+4
source share
4 answers

++ . , , , . , , . std::mutex, .

+3

, ( , - ).

, , - , . ( , )

+3

, , , , , .

, , , , .

0

int Server::Task(std::string arg) int Server__Task(Server* this, std::string arg). , , member - .

The mutex ensures that no conflicting changes are made, and that every thread sees every previous change. But since the code is not random, you do not need a mutex, just as you do not need a mutex for string literals.

0
source

All Articles