Skip to content

Commit

Permalink
fix document errors on locks usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
hgoldfish committed Sep 1, 2022
1 parent be93def commit 6042574
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 7 deletions.
15 changes: 9 additions & 6 deletions docs/references.rst
Original file line number Diff line number Diff line change
Expand Up @@ -456,27 +456,30 @@ The most significant advantage of QtNetworkNg with respect to `boost::coroutine`
.. code-block:: c++
:caption: using RLock

#include "qtnetworkng/qtnetworkng.h"

#include "qtnetworkng.h"

using namespace qtng;

void output(QSharedPointer<RLock> lock, const QString &name)
{
ScopedLock l(lock); // acquire lock now, release before function returns. comment out this line and try again later.
ScopedLock<RLock> l(*lock); // acquire lock now, release before function returns. comment out this line and try again later.
qDebug() << name << 1;
Coroutine::sleep(1.0);
qDebug() << name << 2;
lock.release();
}


int main(int argc, char **argv)
{
QSharedPointer<RLock> lock(new RLock);
QCoroutineGroup operations;
CoroutineGroup operations;
operations.spawn([lock]{
output(lock, "first");
});
operations.spawn([lock]{
output(lock, "second");
});
operations.joinall();
return 0;
}
Expand Down
23 changes: 23 additions & 0 deletions examples/coroutines/coroutines.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
######################################################################
# Automatically generated by qmake (3.1) Wed Oct 17 09:50:03 2018
######################################################################

TEMPLATE = app
TARGET = coroutines
CONFIG += console

# The following define makes your compiler warn you if you use any
# feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

include(../../qtnetworkng.pri)

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0

# Input
SOURCES += main.cpp
35 changes: 35 additions & 0 deletions examples/coroutines/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <QtCore/qcoreapplication.h>
#include "qtnetworkng.h"


using namespace qtng;


struct MyCoroutine: public Coroutine
{
MyCoroutine(const QString &name)
: name(name) {}
void run() override {
for (int i = 0; i < 3; ++i) {
qDebug() << name << i;
// switch to eventloop coroutine, will switch back in 100 ms.
msleep(100);
}
}
QString name;
};


int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
MyCoroutine coroutine1("coroutine1");
MyCoroutine coroutine2("coroutine2");
coroutine1.start();
coroutine2.start();
// switch to the main coroutine
coroutine1.join();
// switch to the second coroutine to finish it.
coroutine2.join();
return 0;
}
23 changes: 23 additions & 0 deletions examples/locks/locks.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
######################################################################
# Automatically generated by qmake (3.1) Wed Oct 17 09:50:03 2018
######################################################################

TEMPLATE = app
TARGET = locks
CONFIG += console

# The following define makes your compiler warn you if you use any
# feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

include(../../qtnetworkng.pri)

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0

# Input
SOURCES += main.cpp
26 changes: 26 additions & 0 deletions examples/locks/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "qtnetworkng.h"

using namespace qtng;

void output(QSharedPointer<RLock> lock, const QString &name)
{
ScopedLock<RLock> l(*lock); // acquire lock now, release before function returns. comment out this line and try again later.
qDebug() << name << 1;
Coroutine::sleep(1.0);
qDebug() << name << 2;
}


int main(int argc, char **argv)
{
QSharedPointer<RLock> lock(new RLock);
CoroutineGroup operations;
operations.spawn([lock]{
output(lock, "first");
});
operations.spawn([lock]{
output(lock, "second");
});
operations.joinall();
return 0;
}
2 changes: 1 addition & 1 deletion src/httpd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ static const QString DEFAULT_ERROR_MESSAGE = QString::fromLatin1("<!DOCTYPE HTML
" <p>Message: %2.</p>\n"
" <p>Error code explanation: %1 - %3.</p>\n"
" </body>\n"
"</html>");
"</html>\n");
static const QString DEFAULT_ERROR_CONTENT_TYPE = QString::fromLatin1("text/html;charset=utf-8");


Expand Down

0 comments on commit 6042574

Please sign in to comment.