What happens if a multithreaded function uses non-multithreaded function ?

i.e
prime_counter is used as multithreaded function
1
2
std::thread first(prime_counter);
std::tread second(prime_counter);


and both of them uses a function called check_function, so what will happen ?
Any thread can potentially access any object in the program (objects with automatic and thread-local storage duration may still be accessed by another thread through a pointer or by reference).

Different threads of execution are always allowed to access (read and modify) different memory locations concurrently, with no interference and no synchronization requirements.

When an evaluation of an expression writes to a memory location and another evaluation reads or modifies the same memory location, the expressions are said to conflict. A program that has two conflicting evaluations has a data race unless
. both evaluations execute on the same thread or in the same signal handler, or
. both conflicting evaluations are atomic operations (see std::atomic), or
. one of the conflicting evaluations happens-before another (see std::memory_order)

If a data race occurs, the behavior of the program is undefined.

(in particular, release of a std::mutex is synchronized-with, and therefore, happens-before acquisition of the same mutex by another thread, which makes it possible to use mutex locks to guard against data races)
http://en.cppreference.com/w/cpp/language/memory_model#Threads_and_data_races
thank you
There are sort of 2 questions here.
1) what happens when you call a function.
-- Each function has its own memory space, including multiple calls of the same function. This is often seen by understanding recursion, where the function suspends activity, saves its current state, calls itself (new memory, new instance of the code), and so on. Do you understand this?

2) threading, which was answered above. While calling the same function is inherently safe due to each call having its own memory and space, if you shared any data across the calls (pointers, containers, static variables, global variables, things like that... there are many ways to share) then that shared memory is at risk as described.

We can't tell from your words if the functions involved share anything and need a mutex or not.



Each function has its own memory space, including multiple calls of the same function
To be clear, each function has it's own parameters, return value and local automatic variables, but everything(?) else is shared.
This is often seen by understanding recursion, where the function [...] calls itself (new memory, new instance of the code),
A recursive call gets new memory for local variables, but the code instance is still the same. You can see this by printing the address of the function. It will be the same for each recursive all.

In fact, a program's code is often shared between different processes too, at least on UNIX-based systems. If you execute two copies of a program, the OS will likely store just one copy of the code in RAM and let both instances use it.
Topic archived. No new replies allowed.