成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

QT 多線程和 QSocket 網絡編程實例解析

移動開發
Qt的signal/slot的事件機制都是基于主程序的線程的,因此所有的事件都是阻塞型的(blocking),也就是說除非你處理完某個slot事件,不然不會有下個事件被觸發。

本文介紹的是QT 多線程QSocket 網絡編程實例解析,要實現網絡編程,不說這么多,先來看內容。

(1) 帶后綴-mt的庫才是支持多線程的.

例如windows下面的qt-mt320.lib,其他平臺libqt-mt

(2)編譯問題,要添加QT_THREAD_SUPPORT

(30針對線程里面而言,blocking(阻塞的) = synchronous(同步的 )

non-blocking (非阻塞的)  = asynchronous(異步的 )

Qt的signal/slot的事件機制都是基于主程序的線程的,因此所有的事件都是阻塞型的(blocking),也就是說除非你處理完某個slot事件,不然不會有下個事件被觸發。

(4)QSocket,QSocketNotifier不能和QThread一起使用

  1. QSocket is for non-blocking IO, it uses some polling like poll() or select() internally and notifies the actual code by emitting signals.  
  2. So QSocket is for use with only the event loop thread, for example in a client with only one socket or a server with very few connections.  
  3. If you want a threaded approach use QThread and QSocketDevice.  
  4. Put one SocketDevice into listening mode and on accept() create a Handler Thread for the socket file descriptor. 
  5. Use the second QSocketDevice constructor to initialise the Connection's socket device instance.  
  6. The server does  
  7. bind()  
  8. listen()  
  9. and  
  10. accept()  
  11. When accept returns it has the filedescriptor of the connection's socket which you can pass to another QSocketDevice constructor.  
  12. The client does connect() to establish the connection.  
  13. Both use readBlock/writeBlock/waitForMore to transfer data 

.

一個例子:

(1)用VC6.0新建個Win32 Console Application工程

(2)Project Settings里面Link標簽頁面添加qtmain.lib qt-mt320.lib

   Project Settings里面C/C++標簽頁面添加QT_THREAD_SUPPORT

(3)源代碼文件(main.cpp):

  1. #include <qthread.h> 
  2. class MyThread : public QThread   
  3. {      
  4. public:  
  5.     virtual void run();  
  6. };  
  7. void MyThread::run()  
  8. {  
  9.     for( int count = 0; count < 20; count++ )   
  10.     {  
  11.         sleep( 1 );  
  12.         qDebug( "Ping!" );  
  13.     }  
  14. }  
  15. int main()  
  16. {  
  17.     MyThread a;  
  18.     MyThread b;  
  19.     a.start();  
  20.     b.start();  
  21.     a.wait();  
  22.     b.wait();  

注釋:

This will start two threads, each of which writes Ping! 20 times to the screen and exits.

The wait() calls at the end of main() are necessary because exiting main() ends the program,

unceremoniously killing all other threads.

Each MyThread stops executing when it reaches the end of MyThread::run(),

just as an application does when it leaves main().

小結:關于QT 多線程QSocket 網絡編程實例解析的內容介紹到這,希望本文對你有所幫助。

責任編輯:zhaolei 來源: 互聯網
相關推薦

2011-06-13 10:03:19

Qt 多線程 編程

2011-06-30 17:31:32

Qt 多線程 信號

2011-06-16 10:38:13

Qt多線程編程

2023-06-13 13:39:00

多線程異步編程

2009-03-12 10:52:43

Java線程多線程

2011-06-30 17:40:07

Linux 多線程 Android

2011-06-20 13:43:08

Qt Socket 線程

2011-06-22 14:30:44

QT 多線程 線程

2011-07-01 11:18:50

Qt 多線程

2011-06-22 14:38:09

QT 多線程 線程安全

2013-07-16 10:12:14

iOS多線程多線程概念多線程入門

2021-03-01 11:20:13

網絡安全多線程代碼

2011-06-13 16:51:19

Qt Socket

2011-06-02 17:27:49

iphone 多線程

2023-11-01 11:20:57

2011-06-22 10:12:08

Qt 線程

2011-07-01 10:35:20

QT 多線程 TCP

2024-03-27 08:47:10

Python多線程Threading

2011-06-22 15:50:45

QT 線程

2011-06-30 18:15:36

Qt 線程 同步
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 毛片片| av片网 | 欧洲一区二区在线 | 久久av一区二区三区 | 欧美三级电影在线播放 | 成年无码av片在线 | av片免费 | 欧美一区二区三区在线 | 亚洲视频免费在线观看 | 精品丝袜在线 | 作爱视频免费观看 | 国产2区| 久久久久网站 | 亚洲网一区 | 超碰成人在线观看 | 亚洲a网 | 成人毛片在线视频 | 国产午夜精品一区二区三区嫩草 | 欧美精品一区在线发布 | 国产美女久久 | 成人激情视频 | 午夜小电影 | 欧美国产日韩在线观看 | 国产精品一区在线观看 | 五月综合久久 | 久草免费在线视频 | 国产日韩欧美一区二区在线播放 | 久久蜜桃精品 | 亚洲成人精选 | 99re在线 | 91亚洲国产精品 | 一级毛片在线播放 | 日日夜夜狠狠操 | 丝袜 亚洲 另类 欧美 综合 | 日韩视频在线免费观看 | 成人免费观看男女羞羞视频 | 久久久做 | 亚洲在线免费 | 成人在线观看网址 | 久久久久国产一区二区三区 | 天堂va在线|