当前位置: 首页>>代码示例>>C++>>正文


C++ QSemaphore::available方法代码示例

本文整理汇总了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();
    }
}
开发者ID:embicoin,项目名称:Foundations.of.Qt.Development,代码行数:13,代码来源:textproducerconsumer.cpp

示例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);
    }
}
开发者ID:gxliu,项目名称:imx,代码行数:14,代码来源:nodeprotocolobject.cpp

示例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();
    }
}
开发者ID:barry963,项目名称:Robot_project,代码行数:42,代码来源:net_radio_send_thread.cpp

示例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 )
//.........这里部分代码省略.........
开发者ID:HezhouW,项目名称:SQL-whz,代码行数:101,代码来源:SqlwriteThread_buffer.cpp

示例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);
}
开发者ID:MarianMMX,项目名称:MarianMMX,代码行数:63,代码来源:tst_qsemaphore.cpp

示例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
}
开发者ID:MarianMMX,项目名称:MarianMMX,代码行数:93,代码来源:tst_qsemaphore.cpp

示例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);
}
开发者ID:MarianMMX,项目名称:MarianMMX,代码行数:51,代码来源:tst_qsemaphore.cpp

示例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);
}
开发者ID:Mr-Kumar-Abhishek,项目名称:qt,代码行数:79,代码来源:tst_qsemaphore.cpp

示例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();
		}
开发者ID:maelvalais,项目名称:cameroa,代码行数:66,代码来源:main.cpp

示例10:

 ~AutoSem() {
     if (s->available() > 0)
         s->acquire(s->available());
 }
开发者ID:JingSao,项目名称:QtAV,代码行数:4,代码来源:AVDemuxThread.cpp


注:本文中的QSemaphore::available方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。