Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Toutes les fonctions  ·  Vues d'ensemble  · 

QSharedMemory Class Reference
[QtCore module]

The QSharedMemory class provides access to a shared memory segment. More...

 #include <QSharedMemory>

Inherits QObject.

This class was introduced in Qt 4.4.


Public Types

enum AccessMode { ReadOnly, ReadWrite }
enum SharedMemoryError { NoError, PermissionDenied, InvalidSize, KeyError, ..., UnknownError }

Public Functions

QSharedMemory ( const QString & key, QObject * parent = 0 )
QSharedMemory ( QObject * parent = 0 )
~QSharedMemory ()
bool attach ( AccessMode mode = ReadWrite )
const void * constData () const
bool create ( int size, AccessMode mode = ReadWrite )
void * data ()
const void * data () const
bool detach ()
SharedMemoryError error () const
QString errorString () const
bool isAttached () const
QString key () const
bool lock ()
void setKey ( const QString & key )
int size () const
bool unlock ()
  • 29 public functions inherited from QObject

Additional Inherited Members

  • 1 property inherited from QObject
  • 1 public slot inherited from QObject
  • 1 signal inherited from QObject
  • 5 static public members inherited from QObject
  • 7 protected functions inherited from QObject

Detailed Description

The QSharedMemory class provides access to a shared memory segment.

QSharedMemory provides access to a shared memory segment by multiple threads and processes. It also provides a way for a single thread or process to lock the memory for exclusive access.

When using this class, be aware of the following platform differences:

  • Windows: QSharedMemory does not "own" the shared memory segment. When all threads or processes that have an instance of QSharedMemory attached to a particular shared memory segment have either destroyed their instance of QSharedMemory or exited, the Windows kernel releases the shared memory segment automatically.
  • Unix: QSharedMemory "owns" the shared memory segment. When the last thread or process that has an instance of QSharedMemory attached to a particular shared memory segment detaches from the segment by destroying its instance of QSharedMemory, the Unix kernel release the shared memory segment. But if that last thread or process crashes without running the QSharedMemory destructor, the shared memory segment survives the crash.
  • HP-UX: Only one attach to a shared memory segment is allowed per process. This means that QSharedMemory should not be used across multiple threads in the same process in HP-UX.
  • Symbian: QSharedMemory does not "own" the shared memory segment. When all threads or processes that have an instance of QSharedMemory attached to a particular shared memory segment have either destroyed their instance of QSharedMemory or exited, the Symbian kernel releases the shared memory segment automatically. Also, access to a shared memory segment cannot be limited to read-only in Symbian.

Remember to lock the shared memory with lock() before reading from or writing to the shared memory, and remember to release the lock with unlock() after you are done.

Unlike QtSharedMemory, QSharedMemory automatically destroys the shared memory segment when the last instance of QSharedMemory is detached from the segment, and no references to the segment remain. Do not mix using QtSharedMemory and QSharedMemory. Port everything to QSharedMemory.

Warning: QSharedMemory changes the key in a Qt-specific way. It is therefore currently not possible to use the shared memory of non-Qt applications with QSharedMemory.


Member Type Documentation

enum QSharedMemory::AccessMode

ConstantValueDescription
QSharedMemory::ReadOnly0The shared memory segment is read-only. Writing to the shared memory segment is not allowed. An attempt to write to a shared memory segment created with ReadOnly causes the program to abort.
QSharedMemory::ReadWrite1Reading and writing the shared memory segment are both allowed.

enum QSharedMemory::SharedMemoryError

ConstantValueDescription
QSharedMemory::NoError0No error occurred.
QSharedMemory::PermissionDenied1The operation failed because the caller didn't have the required permissions.
QSharedMemory::InvalidSize2A create operation failed because the requested size was invalid.
QSharedMemory::KeyError3The operation failed because of an invalid key.
QSharedMemory::AlreadyExists4A create() operation failed because a shared memory segment with the specified key already existed.
QSharedMemory::NotFound5An attach() failed because a shared memory segment with the specified key could not be found.
QSharedMemory::LockError6The attempt to lock() the shared memory segment failed because create() or attach() failed and returned false, or because a system error occurred in QSystemSemaphore::acquire().
QSharedMemory::OutOfResources7A create() operation failed because there was not enough memory available to fill the request.
QSharedMemory::UnknownError8Something else happened and it was bad.


Member Function Documentation

QSharedMemory::QSharedMemory ( const QString & key, QObject * parent = 0 )

Constructs a shared memory object with the given parent and with its key set to key. Because its key is set, its create() and attach() functions can be called.

See also setKey(), create(), and attach().

QSharedMemory::QSharedMemory ( QObject * parent = 0 )

This function overloads QSharedMemory().

Constructs a shared memory object with the given parent. The shared memory object's key is not set by the constructor, so the shared memory object does not have an underlying shared memory segment attached. The key must be set with setKey() before create() or attach() can be used.

See also setKey().

QSharedMemory::~QSharedMemory ()

The destructor clears the key, which forces the shared memory object to detach from its underlying shared memory segment. If this shared memory object is the last one connected to the shared memory segment, the detach() operation destroys the shared memory segment.

See also detach() and isAttached().

bool QSharedMemory::attach ( AccessMode mode = ReadWrite )

Attempts to attach the process to the shared memory segment identified by the key that was passed to the constructor or to a call to setKey(). The access mode is ReadWrite by default. It can also be ReadOnly. Returns true if the attach operation is successful. If false is returned, call error() to determine which error occurred. After attaching the shared memory segment, a pointer to the shared memory can be obtained by calling data().

See also isAttached(), detach(), and create().

const void * QSharedMemory::constData () const

Returns a const pointer to the contents of the shared memory segment, if one is attached. Otherwise it returns null. Remember to lock the shared memory with lock() before reading from or writing to the shared memory, and remember to release the lock with unlock() after you are done.

See also attach() and create().

bool QSharedMemory::create ( int size, AccessMode mode = ReadWrite )

Creates a shared memory segment of size bytes with the key passed to the constructor or set with setKey(), attaches to the new shared memory segment with the given access mode, and returns true. If a shared memory segment identified by the key already exists, the attach operation is not performed, and false is returned. When the return value is false, call error() to determine which error occurred.

See also error().

void * QSharedMemory::data ()

Returns a pointer to the contents of the shared memory segment, if one is attached. Otherwise it returns null. Remember to lock the shared memory with lock() before reading from or writing to the shared memory, and remember to release the lock with unlock() after you are done.

See also attach().

const void * QSharedMemory::data () const

This function overloads data().

bool QSharedMemory::detach ()

Detaches the process from the shared memory segment. If this was the last process attached to the shared memory segment, then the shared memory segment is released by the system, i.e., the contents are destroyed. The function returns true if it detaches the shared memory segment. If it returns false, it usually means the segment either isn't attached, or it is locked by another process.

See also attach() and isAttached().

SharedMemoryError QSharedMemory::error () const

Returns a value indicating whether an error occurred, and, if so, which error it was.

See also errorString().

QString QSharedMemory::errorString () const

Returns a text description of the last error that occurred. If error() returns an error value, call this function to get a text string that describes the error.

See also error().

bool QSharedMemory::isAttached () const

Returns true if this process is attached to the shared memory segment.

See also attach() and detach().

QString QSharedMemory::key () const

Returns the key assigned to this shared memory. The key is the identifier used by the operating system to identify the shared memory segment. When QSharedMemory is used for interprocess communication, the key is how each process attaches to the shared memory segment through which the IPC occurs.

See also setKey().

bool QSharedMemory::lock ()

This is a semaphore that locks the shared memory segment for access by this process and returns true. If another process has locked the segment, this function blocks until the lock is released. Then it acquires the lock and returns true. If this function returns false, it means either that you have ignored a false return from create() or attach(), or that QSystemSemaphore::acquire() failed due to an unknown system error.

See also unlock(), data(), and QSystemSemaphore::acquire().

void QSharedMemory::setKey ( const QString & key )

Sets a new key for this shared memory object. If key and the current key are the same, the function returns without doing anything. If the shared memory object is attached to an underlying shared memory segment, it will detach from it before setting the new key. This function does not do an attach().

See also key() and isAttached().

int QSharedMemory::size () const

Returns the size of the attached shared memory segment. If no shared memory segment is attached, 0 is returned.

See also create() and attach().

bool QSharedMemory::unlock ()

Releases the lock on the shared memory segment and returns true, if the lock is currently held by this process. If the segment is not locked, or if the lock is held by another process, nothing happens and false is returned.

See also lock().

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 44
  2. Microsoft ouvre aux autres compilateurs C++ AMP, la spécification pour la conception d'applications parallèles C++ utilisant le GPU 22
  3. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  4. RIM : « 13 % des développeurs ont gagné plus de 100 000 $ sur l'AppWord », Qt et open-source au menu du BlackBerry DevCon Europe 0
  5. BlackBerry 10 : premières images du prochain OS de RIM qui devrait intégrer des widgets et des tuiles inspirées de Windows Phone 0
  6. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  7. Adieu qmake, bienvenue qbs : Qt Building Suite, un outil déclaratif et extensible pour la compilation de projets Qt 17
Page suivante

Le blog Digia au hasard

Logo

Déploiement d'applications Qt Commercial sur les tablettes Windows 8

Le blog Digia est l'endroit privilégié pour la communication sur l'édition commerciale de Qt, où des réponses publiques sont apportées aux questions les plus posées au support. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 4.6
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web