They are in the same class because fun1() in related with the interface of the object, while fun2() is related to the task that the object has to perform.
fun1() only read some values and then draw some elements, but it cannot change anything.
Only data that is modified across the threads needs to be protected. I.e. when you add something to a vector (or any other container) in one thread and read from this vector in the other thread. Then the vector needs to be protected [with a mutex].
1.You can make some static public functions for the class.
2.Just split them away, think this problem in C way.
And please notice that when using multi-thread. Its memory space is seperated from the main and
1.You`ll need to re-allocate space for the known data to pass through call back correctly.
2.be ware of the run condition on the same resource.(You`ll need to lock the access to some variables or functions)
> fun1() only read some values and then draw some elements, but it cannot change anything.
But presumably it's reading things that fun2() writes to, which makes a complete nonsense of your bold assertion "and that have nothing in common." in your first post.
@c90a78
> And please notice that when using multi-thread. Its memory space is seperated from the main
Not so.
Threads are typically "light-weight" and share the same virtual memory space (code and data). Threads have their own stacks, and perhaps thread local storage.
Processes are typically "heavy-weight" and have different virtual address spaces (even if you run the same program twice). Getting shared data between processes is far from trivial.
Getting shared data between processes is far from trivial.
Depends on what os and what you want to do. Using say a shared data segment for Windows (I don't know about other os's) is quite easy if that is all you want and don't mind the security issues.
Placing C++ objects in memory shared between processes is far from trivial.
The OP only uses shared single variables and not containers. With WIN32 API, we use shared memory for objects that don't use dynamic memory together with named mutex/events and it's quite easy.
#pragma data_seg("Share1")
int ishare1 = 0;
int ishare2 = 0;
#pragma data_seg()
#pragma comment (linker, "/SECTION:Share1,RWS") // Read, write and shared segment
Then sync access to them using named mutex/event as appropriate. Each process just uses the variables ishare1, ishare2. Works fine, with the security caveats....
Placing an object in shared memory is much simpler if the shared memory region is guaranteed to be mapped at the same base address in all processes sharing the memory.
Placing an object of a scalar POD type in shared memory is also very straightforward.
Placing a C++ object (which may have member pointers or references, static member objects or virtual functions) in shared memory is a lot more involved.