本文整理汇总了C++中QSharedMemory::key方法的典型用法代码示例。如果您正苦于以下问题:C++ QSharedMemory::key方法的具体用法?C++ QSharedMemory::key怎么用?C++ QSharedMemory::key使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QSharedMemory
的用法示例。
在下文中一共展示了QSharedMemory::key方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: consumer
int consumer()
{
QSharedMemory consumer;
consumer.setKey("market");
//qDebug("consumer starting");
int tries = 0;
while (!consumer.attach()) {
if (tries == 5000) {
qWarning() << "consumer exiting, waiting too long";
return EXIT_FAILURE;
}
++tries;
QTest::qSleep(1);
}
//qDebug("consumer attached");
int i = 0;
while (true) {
if (!consumer.lock()) {
qWarning() << "Could not lock" << consumer.key();
return EXIT_FAILURE;
}
if (get(consumer, 0) == 'Q') {
set(consumer, 0, ++i);
//qDebug() << "consumer sets" << i;
}
if (get(consumer, 0) == 'E') {
if (!consumer.unlock()) {
qWarning() << "Could not unlock" << consumer.key();
return EXIT_FAILURE;
}
break;
}
if (!consumer.unlock()) {
qWarning() << "Could not unlock" << consumer.key();
return EXIT_FAILURE;
}
QTest::qSleep(10);
}
//qDebug("consumer detaching");
if (!consumer.detach()) {
qWarning() << "Could not detach" << consumer.key();
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
示例2: sipConvertFromNewType
static PyObject *meth_QSharedMemory_key(PyObject *sipSelf, PyObject *sipArgs)
{
PyObject *sipParseErr = NULL;
{
QSharedMemory *sipCpp;
if (sipParseArgs(&sipParseErr, sipArgs, "B", &sipSelf, sipType_QSharedMemory, &sipCpp))
{
QString *sipRes;
Py_BEGIN_ALLOW_THREADS
sipRes = new QString(sipCpp->key());
Py_END_ALLOW_THREADS
return sipConvertFromNewType(sipRes,sipType_QString,NULL);
}
}
示例3: storeSharedData
bool TasDataShare::storeSharedData(const QString& identifier, const TasSharedData& data, QString& errMsg)
{
QSharedMemory* storage = new QSharedMemory(identifier);
QByteArray array = data.asArray();
int size = array.size();
if(!storage->create(size)){
delete storage;
return false;
}
storage->lock();
char *to = (char*)storage->data();
const char *from = array.data();
memcpy(to, from, qMin(storage->size(), size));
storage->unlock();
mStoredDataBlocks.insert(identifier, storage);
errMsg = " key:" + storage->key();
return true;
}
示例4: producer
int producer()
{
QSharedMemory producer;
producer.setKey("market");
int size = 1024;
if (!producer.create(size)) {
if (producer.error() == QSharedMemory::AlreadyExists) {
if (!producer.attach()) {
qWarning() << "Could not attach to" << producer.key();
return EXIT_FAILURE;
}
} else {
qWarning() << "Could not create" << producer.key();
return EXIT_FAILURE;
}
}
// tell parent we're ready
//qDebug("producer created and attached");
puts("");
fflush(stdout);
if (!producer.lock()) {
qWarning() << "Could not lock" << producer.key();
return EXIT_FAILURE;
}
set(producer, 0, 'Q');
if (!producer.unlock()) {
qWarning() << "Could not lock" << producer.key();
return EXIT_FAILURE;
}
int i = 0;
while (i < 5) {
if (!producer.lock()) {
qWarning() << "Could not lock" << producer.key();
return EXIT_FAILURE;
}
if (get(producer, 0) == 'Q') {
if (!producer.unlock()) {
qWarning() << "Could not unlock" << producer.key();
return EXIT_FAILURE;
}
QTest::qSleep(1);
continue;
}
//qDebug() << "producer:" << i);
++i;
set(producer, 0, 'Q');
if (!producer.unlock()) {
qWarning() << "Could not unlock" << producer.key();
return EXIT_FAILURE;
}
QTest::qSleep(1);
}
if (!producer.lock()) {
qWarning() << "Could not lock" << producer.key();
return EXIT_FAILURE;
}
set(producer, 0, 'E');
if (!producer.unlock()) {
qWarning() << "Could not unlock" << producer.key();
return EXIT_FAILURE;
}
//qDebug("producer done");
// Sleep for a bit to let all consumers exit
getchar();
return EXIT_SUCCESS;
}