回顧:
線程與進程
1.進程由於資源獨立,進程的主要關注是解決資源共用
2.線程先天是資料共用,線程主要關注共用資料髒
1.互斥量(線程內)
讀寫鎖
2.訊號/條件量調度(線程之間)
訊號量
一.線程的訊號量
1.定義訊號量sem_t
2.初始化訊號量sem_init
3.操作訊號量sem_post sem_wait
4.釋放訊號量sem_destroy
5.其他函數sem_getvalue
案例:
建立一個子線程
子線程-訊號量(阻塞)
主線程+訊號量(解除阻塞)
體會:
訊號量可以累加
+-操作單位是1
View Code
#include <pthread.h>#include <semaphore.h>#include <stdio.h>sem_t sem;//void *run(void*d){ while(1) { //3. sem_wait(&sem); printf("解除\n"); }}main(){ pthread_t tid; //2 sem_init(&sem,0,5); pthread_create(&tid,0,run,0); while(1) { // sleep(1); sem_post(&sem); }}
一開始列印5次,然後每隔一秒列印一次。
作業:
搖獎程式,繼續暫停7位隨機數
訊號量/條件量/訊號?
目的:選擇哪種技術實現?
1.無法使用互斥與讀寫鎖實現.
2.訊號實現不穩定.
3.沒有互斥建議不考慮條件量.
4.最佳選擇號誌.
二.線程的讀寫鎖(自學)
pthread_rwlock_t;
pthread_rwlock_init;
pthread_rwlock_destroy;
pthread_rwlock_rdlock;
pthread_rwlock_wrlock;
pthread_rwlock_unlock;
讀讀相容
寫寫互斥
讀寫互斥
作用主要是為IO操作設計.
三.線程私人資料(瞭解)
pthread_key_create
pthread_key_delete
四.QT
1.工具
assistant 協助文檔
qtconfig QT組態工具
qmake QT的make與專案檔智能建立工具
uic UI介面的設計檔案的編譯工具
moc moc代碼建立工具
designer UI設計工具
2.QT 的模組
qtcore
qtgui
qtnetwork
qtsvg
qtopengl
3.第一QT程式
1.QT編程模型
2.QT程式編譯過程
3.QT的項目組織
View Code
#include <QApplication>#include <QDialog>int main(int args,char **argv){ //qmake -project //qmake **.pro //make //執行程式 QApplication app(args,argv); //將main的參數傳進去 //////////////////////////// //在這裡可以隨便建立表單 QDialog dlg; dlg.setVisible(true); //////////////////////////// return app.exec();//等待所有子線程結束,才能結束}
注意:
1.QT是C++程式.
2.QT程式要啟動QT的環境QApplication
3.編程的模型
qmake -project
qmake *.pro
make
執行
4.*.pro檔案
TEMPLATE=
:app 應用程式
:lib 庫
SOURCES=
:cpp檔案1 cpp檔案2 cpp檔案3 ....
:\ 續行符號
:SOURCES+=
HEADERS=
:h標頭檔
CONFIG= 影響gcc的選項
:release | debug
:warn_on | warn_off -Wall -w
:qt | opengl
:shared | static
QT= (該變數在CONFIG=qt才有意義)
:core
:gui
:network
:opengl
:svg
:xml
:sql
TARGET=
:輸出檔案名 (-o輸出檔案)
LIBS=使用者指定庫
:-l -L
FORMS=ui檔案
小例子:
View Code
TEMPLATE=appSOURCES=main.cppHEADERS=CONFIG=qt releaseQT=core guiTARGET=mainLIBS=-L.
5.怎麼學習QT
QT類的結構層次.
QT的訊號signal與槽slot.
QT基本組件
QT的UI設計器
五.直觀認識QT
封裝與協助文檔的使用
1.文檔的組成部分:
1.模組
2.類作用簡單描述
3.標頭檔
4.父類與衍生類別
5.構造器/析構器()
6.共有函數
7.屬性
2.亂碼處理
QT提供翻譯機制
QTextCodec
View Code
#include <QApplication>#include <QDialog>#include <QPushButton>#include <QTextCodec>#include <QLineEdit>int main(int args,char**argv){ QApplication app(args,argv); QTextCodec *codec=QTextCodec::codecForName("gb2312"); QTextCodec::setCodecForTr(codec); QDialog dlg; dlg.resize(400,300); dlg.move((1024-400)/2,(768-300)/2); QPushButton btn(&dlg); btn.resize(100,30); btn.move(100,100); btn.setText(QObject::tr("確定")); //轉碼,防止亂碼 QLineEdit edt(&dlg); edt.resize(100,30); edt.move(100,200); edt.setText(QObject::tr("確定")); dlg.setVisible(true); return app.exec();}
pro檔案的編寫:
View Code
TEMPLATE=appSOURCES=main.cppCONFIG=release qtQT=core guiTARGET=main
3.程式碼群組織
以表單為基本單位的封裝
案例:
登入
QDialog
QPushButton
QLabel
QLineEdit
View Code
//logindlg.h#ifndef LOGIN_DLG_H#define LOGIN_DLG_H#include <QDialog> #include <QLabel>#include <QLineEdit>#include <QPushButton>class LoginDialog : public QDialog{private: QLabel *lbluser; QLabel *lblpass; QLineEdit *edtuser; QLineEdit *edtpass; QPushButton *btnlogin; QPushButton *btncancel; public: LoginDialog(QWidget *parent=NULL);};#endifView Code
//logindlg.cpp#include "logindlg.h"LoginDialog::LoginDialog(QWidget *parent) :QDialog(parent){ resize(400,300); move((1024-400)/2,(768-300)/2); lbluser=new QLabel(this); lblpass=new QLabel(this); lbluser->resize(100,30); lbluser->move(50,40); lblpass->resize(100,30); lblpass->move(50,100); lbluser->setText(QObject::tr("使用者:")); lblpass->setText(QObject::tr("口令:")); edtuser=new QLineEdit(this); edtpass=new QLineEdit(this); edtuser->resize(200,30); edtuser->move(150,40); edtpass->resize(200,30); edtpass->move(150,100); btnlogin=new QPushButton(this); btncancel=new QPushButton(this); btnlogin->resize(60,30); btnlogin->move(90,210); btncancel->resize(60,30); btncancel->move(250,210); btnlogin->setText(QObject::tr("登入")); btncancel->setText(QObject::tr("取消"));}View Code
//main.cpp#include "logindlg.h"#include <QApplication>#include <QTextCodec>int main(int args,char**argv){ QApplication app(args,argv); QTextCodec *codec =QTextCodec::codecForName("gb2312"); QTextCodec::setCodecForTr(codec); LoginDialog dlg; dlg.resize(400,300); dlg.setVisible(true); return app.exec();}
4.QT的介面設計器
designer
4.1.工具視圖:
使用者設計區
工具箱
資源管理員
屬性編輯器(ctrl+i)
動作編輯器
訊號槽編輯器
對象察看器(選擇對象)
4.2.儲存檔案
*.ui
4.3.編輯屬性
1.文字屬性
2.對象名屬性
3.字型屬性
4.4.編輯組件
-> <-方向
shift+
ctrl+
shift+ctrl+
4.5.開啟ui檔案
ui是xml文字檔
4.6.使用ui檔案
自動使用
手工使用
uic編譯ui檔案
產生:Ui_對象名的類
Ui::對象名的類
類的構造器:沒有(預設構造器)
類的方法:setUpUi(QDialog *);
4.7.使用UI類型
作業:
1.完成計算機的介面
思考:
2.建立線程?不停調用文本組件的setText()修改文本?