-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
117 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters