initialization - Using Critical Sections/Semaphores in C++ -


i started using c++ instead of delphi. , there things seem quite different. example don't know how initialize variables semaphores , criticalsections. know 2 possible ways: 1. initializing critical section in constructor stupid since every instance using own critical section without synchronizing anything, right? 2. using global var , initializing when form created seems not perfect solution, well. can tell me how achieve this?

just short explanation need critical section : i'd fill listbox different threads. semaphore : different threads moving mouse, shouldn't interrupted.

thanks!

contrary delphi, c++ has no concept of unit initialization/finalization (but found out that).

what left little. need distinguish 2 things:

  • where declare variable (global, static class member, class member, local function, static in function -- guess covers all)
  • where initialize variable (since concerned c api have call initialization function yourself)

fact is, in case hardly matters declare variable long accessible other parts of program need it, , requirement should initialize is: before start using (which implies, before start other threads).

in case use singleton pattern. c++ being is, singletons suffer race condition during initialization, there no clean way around that. so, in addition singleton, should ensure correctly created before start using in multithreaded context. simple call getinstance() @ start of main() trick (or anywhere else see fit). see, takes care of declare variable, not initialize it, unfortunately c++ has important limitations when comes multithreading (it under-specified) there no way around that.

to sum up: want (as long works) , stop worrying.


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -