本文整理汇总了C++中QSemaphore::available方法的典型用法代码示例。如果您正苦于以下问题:C++ QSemaphore::available方法的具体用法?C++ QSemaphore::available怎么用?C++ QSemaphore::available使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QSemaphore
的用法示例。
在下文中一共展示了QSemaphore::available方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: run
void TextConsumer::run()
{
int i = 0;
// QSemaphore::availableData():
// returns the number of available semaphores
while( atEnd.available() || availableData.available() )
{
availableData.acquire();
qDebug() << buffer[ i ];
i = (i+1) % bufferSize;
freeSpace.release();
}
}
示例2: init
void NodeProtocolObjectPrivate::init()
{
nodeNum = 0;
ctlCode = 0;
cmdType = 0;
cmdNum = 0;
cmdContent = 0;
cmdPramLen = 0;
memset(cmdParam, 0, sizeof(cmdParam));
if(sem.available() > 0)
{
sem.acquire(1);
}
}
示例3: run
void RadioSendThread::run()
{
RadioServer Server;
if (!Server.Open())
{
printf("Server Open failed!\r\n");
return;
}
int size;
while (true)
{
qDebug() << "Available: " << RadioSendRequest.available();
RadioSendRequest.acquire();
if (shutdownView)break;
RadioSendMutex.lock();
RadioCmds.set_time_stamp(0);
size= RadioCmds.cmds_size();
qDebug() << "size:" << size;
qDebug() << "IsInitialized: " << RadioCmds.IsInitialized();
if (RadioCmds.IsInitialized() && size>0 && size<10)
{
RadioCmds.set_time_stamp(0);
Server.Send(RadioCmds);
qDebug() << "sending";
}
else
{
printf("Radio Send Packet Error!\r\n");
}
Server.Send(RadioCmds);
RadioCmds.clear_cmds();
RadioSendMutex.unlock();
RadioSendRequest.release();
}
}
示例4: run
void SqlwriteThread_buffer::run()
{
QString databaseseq;
QSqlDatabase database = QSqlDatabase::addDatabase("QMYSQL",databaseseq.setNum(m_index));
database.setHostName(m_hostname);
database.setDatabaseName(m_databasename);
database.setUserName(m_usrname);
database.setPassword(m_psword);
if (!database.open())
{
database.close();
qDebug()<<trUtf8("数据库打不开")<<endl;
return ;
}
else
{
qDebug()<<trUtf8("线程内数据已经打开")<<endl;
}
/*
QString i2c;
QFile timelog("timelog"+i2c.setNum(m_index)+".txt");
if (!timelog.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append))
{
qDebug()<<trUtf8("LOG日志没有打开")<<endl;
return;
}
timelog.close();
*/
QSqlQuery sqlquery(database);
// QTime timerecord;
// timerecord.start();
qDebug()<<"sqltest thread runing...";
mysqldata *data=g_buffer.data();
mysqldata tempdata;
bool flag = true;
while(g_rIndex < DATABLOCKSIZE)
{
mutex.lock();
if(usedBuffer.tryAcquire())
{
flag = false;
int index=g_rIndex;
++g_rIndex;
qDebug()<<QThread::currentThreadId()<<trUtf8("线程读写第")
<<index<<trUtf8("块数据,now数据缓存区内存有 ")<<usedBuffer.available()+1<<" blocks!";
mutex.unlock(); //先锁住rindex,如果能获取得到资源,就取走该资源。rndex指向该资源。
tempdata = data[index % 10];
//qDebug()<<"-";//
freeBuffer.release();
}
else
{
mutex.unlock();
continue;
}
int range = tempdata.datavector.size();
for(int i=0;i <= range/SQLLINE ; ++i) //i要改成大数,同range
{
QString sqlstatement = "";
int statementnum =( (i+1)*SQLLINE > range ) ? range - i*SQLLINE: SQLLINE;
sqlstatement="insert into InsertTest("
"id,"
"taskseq,"
"protocoltype ,"
"threadnumintask,"
"idinthread,"
"framesize,"
"DestMac,"
"SourMac,"
"TypeorLength,"
"Data,"
"Upperlayer) VALUES";
for(int j=0; j < statementnum; j++)
{
sqlstatement += "("+tempdata.idvector.at(i*SQLLINE + j);
sqlstatement += "," + tempdata.taskseqvector.at(i*SQLLINE + j);
sqlstatement += ", '" + tempdata.typevector.at(i*SQLLINE + j)+"'";
sqlstatement += "," + tempdata.threadnumvector.at(i*SQLLINE + j);
sqlstatement += "," + tempdata.idinthreadvector.at(i*SQLLINE + j);
sqlstatement += "," + tempdata.framesizevector.at(i*SQLLINE + j);
sqlstatement += ", '" + tempdata.DestMacvector.at(i*SQLLINE + j)+"'";
sqlstatement += ", '" + tempdata.SourMacvector.at(i*SQLLINE + j)+"'";
sqlstatement += ", '" + tempdata.TypeorLengthvector.at(i*SQLLINE + j)+"'";
sqlstatement += ", '" + tempdata.datavector.at(i*SQLLINE + j)+"'";
sqlstatement += ", '" + tempdata.uplayervector.at(i*SQLLINE + j)+"')";
if( j == statementnum -1) sqlstatement += ";";
else sqlstatement += ",";
}
if( statementnum != 0 )
//.........这里部分代码省略.........
示例5: acquire
void tst_QSemaphore::acquire()
{
{
// old incrementOne() test
QVERIFY(!semaphore);
semaphore = new QSemaphore;
// make some "thing" available
semaphore->release();
ThreadOne t1;
ThreadOne t2;
t1.start();
t2.start();
QVERIFY(t1.wait(4000));
QVERIFY(t2.wait(4000));
delete semaphore;
semaphore = 0;
}
// old incrementN() test
{
QVERIFY(!semaphore);
semaphore = new QSemaphore;
// make 4 "things" available
semaphore->release(4);
ThreadN t1(2);
ThreadN t2(3);
t1.start();
t2.start();
QVERIFY(t1.wait(4000));
QVERIFY(t2.wait(4000));
delete semaphore;
semaphore = 0;
}
QSemaphore semaphore;
QCOMPARE(semaphore.available(), 0);
semaphore.release();
QCOMPARE(semaphore.available(), 1);
semaphore.release();
QCOMPARE(semaphore.available(), 2);
semaphore.release(10);
QCOMPARE(semaphore.available(), 12);
semaphore.release(10);
QCOMPARE(semaphore.available(), 22);
semaphore.acquire();
QCOMPARE(semaphore.available(), 21);
semaphore.acquire();
QCOMPARE(semaphore.available(), 20);
semaphore.acquire(10);
QCOMPARE(semaphore.available(), 10);
semaphore.acquire(10);
QCOMPARE(semaphore.available(), 0);
}
示例6: tryAcquireWithTimeout
void tst_QSemaphore::tryAcquireWithTimeout()
{
QFETCH(int, timeout);
// timers are not guaranteed to be accurate down to the last millisecond,
// so we permit the elapsed times to be up to this far from the expected value.
int fuzz = 50;
QSemaphore semaphore;
QElapsedTimer time;
#define FUZZYCOMPARE(a,e) \
do { \
int a1 = a; \
int e1 = e; \
QVERIFY2(qAbs(a1-e1) < fuzz, \
qPrintable(QString("(%1=%2) is more than %3 milliseconds different from (%4=%5)") \
.arg(#a).arg(a1).arg(fuzz).arg(#e).arg(e1))); \
} while (0)
QCOMPARE(semaphore.available(), 0);
semaphore.release();
QCOMPARE(semaphore.available(), 1);
time.start();
QVERIFY(!semaphore.tryAcquire(2, timeout));
FUZZYCOMPARE(time.elapsed(), timeout);
QCOMPARE(semaphore.available(), 1);
semaphore.release();
QCOMPARE(semaphore.available(), 2);
time.start();
QVERIFY(!semaphore.tryAcquire(3, timeout));
FUZZYCOMPARE(time.elapsed(), timeout);
QCOMPARE(semaphore.available(), 2);
semaphore.release(10);
QCOMPARE(semaphore.available(), 12);
time.start();
QVERIFY(!semaphore.tryAcquire(100, timeout));
FUZZYCOMPARE(time.elapsed(), timeout);
QCOMPARE(semaphore.available(), 12);
semaphore.release(10);
QCOMPARE(semaphore.available(), 22);
time.start();
QVERIFY(!semaphore.tryAcquire(100, timeout));
FUZZYCOMPARE(time.elapsed(), timeout);
QCOMPARE(semaphore.available(), 22);
time.start();
QVERIFY(semaphore.tryAcquire(1, timeout));
FUZZYCOMPARE(time.elapsed(), 0);
QCOMPARE(semaphore.available(), 21);
time.start();
QVERIFY(semaphore.tryAcquire(1, timeout));
FUZZYCOMPARE(time.elapsed(), 0);
QCOMPARE(semaphore.available(), 20);
time.start();
QVERIFY(semaphore.tryAcquire(10, timeout));
FUZZYCOMPARE(time.elapsed(), 0);
QCOMPARE(semaphore.available(), 10);
time.start();
QVERIFY(semaphore.tryAcquire(10, timeout));
FUZZYCOMPARE(time.elapsed(), 0);
QCOMPARE(semaphore.available(), 0);
// should not be able to acquire more
time.start();
QVERIFY(!semaphore.tryAcquire(1, timeout));
FUZZYCOMPARE(time.elapsed(), timeout);
QCOMPARE(semaphore.available(), 0);
time.start();
QVERIFY(!semaphore.tryAcquire(1, timeout));
FUZZYCOMPARE(time.elapsed(), timeout);
QCOMPARE(semaphore.available(), 0);
time.start();
QVERIFY(!semaphore.tryAcquire(10, timeout));
FUZZYCOMPARE(time.elapsed(), timeout);
QCOMPARE(semaphore.available(), 0);
time.start();
QVERIFY(!semaphore.tryAcquire(10, timeout));
FUZZYCOMPARE(time.elapsed(), timeout);
QCOMPARE(semaphore.available(), 0);
#undef FUZZYCOMPARE
}
示例7: tryAcquire
void tst_QSemaphore::tryAcquire()
{
QSemaphore semaphore;
QCOMPARE(semaphore.available(), 0);
semaphore.release();
QCOMPARE(semaphore.available(), 1);
QVERIFY(!semaphore.tryAcquire(2));
QCOMPARE(semaphore.available(), 1);
semaphore.release();
QCOMPARE(semaphore.available(), 2);
QVERIFY(!semaphore.tryAcquire(3));
QCOMPARE(semaphore.available(), 2);
semaphore.release(10);
QCOMPARE(semaphore.available(), 12);
QVERIFY(!semaphore.tryAcquire(100));
QCOMPARE(semaphore.available(), 12);
semaphore.release(10);
QCOMPARE(semaphore.available(), 22);
QVERIFY(!semaphore.tryAcquire(100));
QCOMPARE(semaphore.available(), 22);
QVERIFY(semaphore.tryAcquire());
QCOMPARE(semaphore.available(), 21);
QVERIFY(semaphore.tryAcquire());
QCOMPARE(semaphore.available(), 20);
QVERIFY(semaphore.tryAcquire(10));
QCOMPARE(semaphore.available(), 10);
QVERIFY(semaphore.tryAcquire(10));
QCOMPARE(semaphore.available(), 0);
// should not be able to acquire more
QVERIFY(!semaphore.tryAcquire());
QCOMPARE(semaphore.available(), 0);
QVERIFY(!semaphore.tryAcquire());
QCOMPARE(semaphore.available(), 0);
QVERIFY(!semaphore.tryAcquire(10));
QCOMPARE(semaphore.available(), 0);
QVERIFY(!semaphore.tryAcquire(10));
QCOMPARE(semaphore.available(), 0);
}
示例8: tryAcquireWithTimeout
void tst_QSemaphore::tryAcquireWithTimeout()
{
QFETCH(int, timeout);
QSemaphore semaphore;
QTime time;
QCOMPARE(semaphore.available(), 0);
semaphore.release();
QCOMPARE(semaphore.available(), 1);
time.start();
QVERIFY(!semaphore.tryAcquire(2, timeout));
QVERIFY(time.elapsed() >= timeout);
QCOMPARE(semaphore.available(), 1);
semaphore.release();
QCOMPARE(semaphore.available(), 2);
time.start();
QVERIFY(!semaphore.tryAcquire(3, timeout));
QVERIFY(time.elapsed() >= timeout);
QCOMPARE(semaphore.available(), 2);
semaphore.release(10);
QCOMPARE(semaphore.available(), 12);
time.start();
QVERIFY(!semaphore.tryAcquire(100, timeout));
QVERIFY(time.elapsed() >= timeout);
QCOMPARE(semaphore.available(), 12);
semaphore.release(10);
QCOMPARE(semaphore.available(), 22);
time.start();
QVERIFY(!semaphore.tryAcquire(100, timeout));
QVERIFY(time.elapsed() >= timeout);
QCOMPARE(semaphore.available(), 22);
time.start();
QVERIFY(semaphore.tryAcquire(1, timeout));
QVERIFY(time.elapsed() <= timeout);
QCOMPARE(semaphore.available(), 21);
time.start();
QVERIFY(semaphore.tryAcquire(1, timeout));
QVERIFY(time.elapsed() <= timeout);
QCOMPARE(semaphore.available(), 20);
time.start();
QVERIFY(semaphore.tryAcquire(10, timeout));
QVERIFY(time.elapsed() <= timeout);
QCOMPARE(semaphore.available(), 10);
time.start();
QVERIFY(semaphore.tryAcquire(10, timeout));
QVERIFY(time.elapsed() <= timeout);
QCOMPARE(semaphore.available(), 0);
// should not be able to acquire more
time.start();
QVERIFY(!semaphore.tryAcquire(1, timeout));
QVERIFY(time.elapsed() >= timeout);
QCOMPARE(semaphore.available(), 0);
time.start();
QVERIFY(!semaphore.tryAcquire(1, timeout));
QVERIFY(time.elapsed() >= timeout);
QCOMPARE(semaphore.available(), 0);
time.start();
QVERIFY(!semaphore.tryAcquire(10, timeout));
QVERIFY(time.elapsed() >= timeout);
QCOMPARE(semaphore.available(), 0);
time.start();
QVERIFY(!semaphore.tryAcquire(10, timeout));
QVERIFY(time.elapsed() >= timeout);
QCOMPARE(semaphore.available(), 0);
}
示例9: main
//.........这里部分代码省略.........
// Chargement des consignes sauvegardees
//
if( !FenetrePrincipale->ChargeConsignes() ) exit(EXIT_FAILURE);
}
// Lancement du processus leger serveur reseau des commandes sur le CamerOA
//
ProcessusLegerServeurCommandes PLServeurCommandes(FenetrePrincipale,INADDR_ANY,QString(arguments->getOption(OptionsLC[PortCanalCommandes])).toInt(),AdresseClient.s_addr,2,TIMEOUT_EMISSION,TIMEOUT_RECEPTION,5,FnHandlerSIGPIPECamerOA,arguments->getOption(OptionsLC[MdpClePriveeServeur]),MotDePasseClePriveeServeurCAMEROA,FnMotDePasseClePriveeChiffreeCAMEROA,arguments->getOption(OptionsLC[CheminFichCertCA_OA]),arguments->getOption(OptionsLC[CheminFichCertServCamerOA]),arguments->getOption(OptionsLC[CheminFichClePriveeServCamerOA]),arguments->getOption(OptionsLC[CheminFichParamDH]),"HIGH");
FenetrePrincipale->PLServeurCommandes=&PLServeurCommandes;
PLServeurCommandes.start();
// Lancement du processus leger serveur reseau des donnees sur le CamerOA
//
ProcessusLegerServeurDonnees PLServeurDonnees(FenetrePrincipale,INADDR_ANY,QString(arguments->getOption(OptionsLC[PortCanalDonnees])).toInt(),AdresseClient.s_addr,2,TIMEOUT_EMISSION,TIMEOUT_RECEPTION);
FenetrePrincipale->PLServeurDonnees=&PLServeurDonnees;
PLServeurDonnees.start();
// Les pointeurs entre les processus
//
PLServeurCommandes.threadCanalDonnees=&PLServeurDonnees;
PLServeurCommandes.threadCamera=&PLCamera;
// Lancement du processus leger de controle de la camera
//
PLCamera.start();
// Tant que les threads ne sont pas tous lances et operationnels
//
while( SemaphoreSyncLancementThreadCamerOA.available() > 0 );
// On affiche la fenetre principale
//
FenetrePrincipale->show();
// Demarrage du timer une fois que tout est lance et affiche
//
FenetrePrincipale->Pulsar1s->start(1000,FALSE);
// FenetrePrincipale a un drapeau WDestructiveClose par defaut, elle se detruira elle meme.
//
retour=appli.exec();
// On demande l'arret des processus legers
//
if( PLServeurCommandes.running() )
{
PLServeurCommandes.DemandeTerminaison();
}
if( PLServeurDonnees.running() )
{
PLServeurDonnees.DemandeTerminaison();
}
if( PLCamera.running() )
{
PLCamera.DemandeTerminaison();
}
示例10:
~AutoSem() {
if (s->available() > 0)
s->acquire(s->available());
}