[Qt-interest] QSystemSemaphore::acquire return false with error QSystemSemaphore::OutOfResources after 32767 pair of (release/acquire)
Dong Tiger
idlecat511 at gmail.com
Mon Aug 24 08:49:27 CEST 2009
Hi,
I used QSystemSemaphore to solve producer/consumer problem in my program. I
found a strange problem.
After 32767 acquire operations, the semaphore stopped working. Acquiring
this semaphore returned false
with error QSystemSemaphore::OutOfResources.
I wrote a simple program to demonstrate the problem.You can try it by:
$./a.out c (start the consumer)
$./a.out p (start the producer)
Then you will see producer will exit after producing 32767 products with
error
QSystemSemaphore::OutOfResources. My environment is ubuntu8.04 + (Qt4.4.0 |
Qt4.5.2).
<code>
#include <stdint.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <iostream>
#include <QSystemSemaphore>
#include <QCoreApplication>
static const char *kSemKeyWrite = "TestSemWrite";
static const char *kSemKeyRead = "TestSemRead";
static const int kWriteNum = 1000;
static QSystemSemaphore *sem_read;
static QSystemSemaphore *sem_write;
static uint32_t count = 0;
bool pause_flag = false;
static void SignalHandler(int sig) {
if (pause_flag) {
std::cout << "Resume\n";
} else {
std::cout << "Pause:" << count << "\n";
}
pause_flag = !pause_flag;
}
int main(int argc, char **argv) {
if (argc == 1) {
std::cout << "Please specify mode(p(producer) or c(cousumer)\n";
return 1;
}
signal(SIGINT, SignalHandler);
if (argv[1][0] == 'c') {
sem_read = new QSystemSemaphore(kSemKeyRead, 0,
QSystemSemaphore::Create);
sem_write = new QSystemSemaphore(kSemKeyWrite,
kWriteNum,
QSystemSemaphore::Create);
// for (int i = 0; i < 20000; i++) {
while (1) {
if (!sem_read->acquire()) {
std::cout << sem_read->errorString().toUtf8().data() << ":" << count
<< "\n";
if (sem_read->error()!= QSystemSemaphore::OutOfResources) continue;
return 1;
}
sem_write->release();
count++;
if ((count % 10000) == 0) {
std::cout << "P:" << count << "\n";
}
while (pause_flag) {
usleep(100000);
}
}
} else {
sem_read = new QSystemSemaphore(kSemKeyRead);
sem_write = new QSystemSemaphore(kSemKeyWrite);
// for (int i = 0; i < 10000 + kWriteNum; i++) {
while (1) {
while(!sem_write->acquire()) {
std::cout << sem_write->errorString().toUtf8().data() << ":" <<
count << "\n";
if (sem_write->error()!= QSystemSemaphore::OutOfResources) continue;
return 1;
}
sem_read->release();
count++;
if ((count % 10000) == 0) {
std::cout << "C:" << count << "\n";
}
while (pause_flag) {
usleep(100000);
}
}
}
return 0;
}
</code>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090824/1f266532/attachment.html
More information about the Qt-interest-old
mailing list