c++ - Multithreading and oop -
i working on project uses 2 different threads (th1
, th2
). share several object , variables via extern
keyword. global object , global variables
something like:
extern obj1 *obj1;
it seems me that:
- calling metod of obj1 in different threads;
- setting value of obj1 in different threads;
- using heavily global boolean variables semaphores;
is not safe way of programming, have reference proof it, paper or book discourage it. can clarify? i'm assuming x86 platform being used visual studio c++.
the rule simple: if thread modifies object (including "objects" doubles or pointers), , more 1 thread accesses it, all accesses must protected. defined in c++11 standard, restates rules applied under posix (and far know, under windows well).
beyond that, safe depends on doing. in own code, example, find rare need non-const global objects; logging exception (and there, of objects thread local, shared object used when log message flushed final destination). objects locals, or allocated dynamically, , if ever shared between threads.
Comments
Post a Comment