qt:thread
차이
문서의 선택한 두 판 사이의 차이를 보여줍니다.
다음 판 | 이전 판 | ||
qt:thread [2014/02/19 06:23] – 새로 만듦 changwoo | qt:thread [2014/10/09 21:24] (현재) – 바깥 편집 127.0.0.1 | ||
---|---|---|---|
줄 3: | 줄 3: | ||
===== Documentation ====== | ===== Documentation ====== | ||
* http:// | * http:// | ||
- | * | + | * http:// |
+ | |||
+ | |||
+ | ===== ThreadPool ====== | ||
+ | 각 Qt 어플리케이션은 글로벌하게 스레드 풀을 가지고 있다. 이를 이용해 재사용 가능한 스레드를 생성할 수 있다. 일례로 GUI 어플리케이션에서 어떤 일을 수행하면서도 다른 GUI 파트의 응답을 받을 수 있도록 해야 한다. 그렇게 하려면 어떤 일을 수행하는 코드는 반드시 스레드로 만들어야 응답성이 향상된다. | ||
+ | |||
+ | ===== 간단한 예제 ===== | ||
+ | [[.: | ||
+ | |||
+ | signal_emitter에 다음과 같이 소스를 추가한다. | ||
+ | <code cpp> | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | class ATask : public QRunnable | ||
+ | { | ||
+ | void run() | ||
+ | { | ||
+ | for(int i = 0; i < 10; ++i) { | ||
+ | std::cout << " | ||
+ | QThread:: | ||
+ | } | ||
+ | } | ||
+ | }; | ||
+ | </ | ||
+ | |||
+ | launch() 함수는 다음과 같이 변경한다. | ||
+ | <code cpp> | ||
+ | void signal_emitter:: | ||
+ | { | ||
+ | std::cout << " | ||
+ | |||
+ | ATask *atask = new ATask(); | ||
+ | QThreadPool:: | ||
+ | // | ||
+ | |||
+ | for(int i = 0; i < 10 ; ++i) { | ||
+ | emit send(i); | ||
+ | QThread:: | ||
+ | } | ||
+ | |||
+ | emit finished(); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | 이를 실행하면 다음과 같이 race가 발생한다. | ||
+ | < | ||
+ | Hello, world! | ||
+ | received: 0 | ||
+ | counting in thread: 0 | ||
+ | received: 1 | ||
+ | counting in thread: 1 | ||
+ | received: 2 | ||
+ | counting in thread: 2 | ||
+ | rceocuenitviendg: | ||
+ | 3 | ||
+ | received: 4 | ||
+ | counting in thread: 4 | ||
+ | received: 5 | ||
+ | counting in thread: 5 | ||
+ | received: 6 | ||
+ | counting in thread: 6 | ||
+ | croeucnetiivnegd :i n thread: 7 | ||
+ | 7 | ||
+ | croeucnetiivnegd :i n thread: 8 | ||
+ | 8 | ||
+ | received: 9 | ||
+ | counting in thread: 9 | ||
+ | </ | ||
+ | |||
+ | 주석을 해제하면 | ||
+ | < | ||
+ | Hello, world! | ||
+ | counting in thread: 0 | ||
+ | counting in thread: 1 | ||
+ | counting in thread: 2 | ||
+ | counting in thread: 3 | ||
+ | counting in thread: 4 | ||
+ | counting in thread: 5 | ||
+ | counting in thread: 6 | ||
+ | counting in thread: 7 | ||
+ | counting in thread: 8 | ||
+ | counting in thread: 9 | ||
+ | received: 0 | ||
+ | received: 1 | ||
+ | received: 2 | ||
+ | received: 3 | ||
+ | received: 4 | ||
+ | received: 5 | ||
+ | received: 6 | ||
+ | received: 7 | ||
+ | received: 8 | ||
+ | received: 9 | ||
+ | </ | ||
+ | |||
+ | 이 소스는 {{: |
qt/thread.1392791024.txt.gz · 마지막으로 수정됨: 2014/10/09 21:23 (바깥 편집)