I have a basic question about how to share data between classes. I want to avoid the use of global variables but I'm using semaphore as synchronization object and global counter between threads/classes. Hence I declared the semaphore objects as global variable.
The thing is that I have an application where the synchronization between threads is critical and therefore I have to use a lot of semaphore and sockets as global variables.
I think in this case it doesn't worth that I write all my function in classes because the use of global variables violates the basic encapsulation rule of object oriented programming. So I have two possibilities as I understand the problem:
1) I don't write the threads and its functions in a class, I use it in the main as common functions and I share the semaphore, sockets, etc as global variables
2) I write each thread with its functions in classes and I write the semaphore, sockets, etc in a header file for global variables as a extern (I don't know if I can create a semaphore as a extern variable...) and I include the header file in the classes.
Now I'm using the first one because as I said I don't see the effort to do the second one because global variables philosophy doesn't fit the basic encapsulation rule of object oriented programming.
I need your opinion, can you give some good advice?
Thank you very much