[Qt-interest] Using Semaphores multiple times in different instances of a class

Arnold Krille arnold at arnoldarts.de
Mon Oct 4 16:27:47 CEST 2010


On Monday 04 October 2010 16:10:23 Jannis Achstetter wrote:
> I have an application in C++ that creates multiple instances of a class.
> Every instance shall have its own circular buffer (used to store audio
> samples decoded elsewhere in that instance). An instance of another
> class then calls a function of every decoding-instance to get audio from
> the instance's circular buffer.
> As long there is only one audio stream (one instance of the decoding
> class), everything is fine. As soon as two (ore more) audio streams are
> working with "their" circular buffer, the data gets corrupted.
> To manage the circular buffer, I use QSemaphores as shown in the
> Semaphore Example (Qt Examples -> Threading). Since I do not want them
> global but per-instance, I tried to "declare" the Semaphores in the
> classes header-file as "private" variables. This did not work,
> Semaphores do not behave like normal class instances (the compiler
> throws an error concerning the numeral value in the braces).
> So I declared them in the classes source-code. Since the Semaphores are
> used by several functions of the class, I did not declare them in a
> function itself but outside any function (making them global obiviosly).
> When I try to use the scope ("QSemaphore ClassName::SemName(int)"), the
> compiler throws an error.
> So my question is: How can I implement a class where every instance has
> its own copy of two QSemaphores? Or, if not possible: How do I implement
> a class where every instance has its own circular buffer?

Declare your semaphores in the class (in the header) and instantiate them in 
your constructor with the number of resources. Or call QSemaphore::release(X) 
in the constructor.

Arnold
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20101004/1c56aa30/attachment.bin 


More information about the Qt-interest-old mailing list