国外网站配色平凉哪有做网站的
news/
2025/9/26 16:43:56/
文章来源:
国外网站配色,平凉哪有做网站的,空中乘务专业简历制作,互联网行业黑话【为什么要用多线程#xff1f;】 传统的图形用户界面应用程序都仅仅有一个运行线程#xff0c;而且一次仅仅运行一个操作。假设用户从用户界面中调用一个比較耗时的操作#xff0c;当该操作正在运行时#xff0c;用户界面一般会冻结而不再响应。这个问题能够用事件处理和多… 【为什么要用多线程】 传统的图形用户界面应用程序都仅仅有一个运行线程而且一次仅仅运行一个操作。假设用户从用户界面中调用一个比較耗时的操作当该操作正在运行时用户界面一般会冻结而不再响应。这个问题能够用事件处理和多线程来解决。 【Linux有线程的概念吗】 传统的UNIX系统也支持线程的概念但一个进程里仅仅同意有一个线程这样多线程就是多进程。Linux下的Posix线程pthreads是一种轻量级的进程的移植性实现线程的调度由内核完毕每一个线程都有自己的编号。假设使用线程整体消耗的系统资源较少线程间通信也比較easy在project中推荐使用线程。 【使用多线程有什么优点】 提高应用程序的响应速度。这对于开发图形界面程序尤其重要当一个操作耗时非常长时比方大批量I/O或大量矩阵变换等CPU密集操作整个系统都会等待这个操作程序就不能响应键盘、鼠标、菜单等操作而使用多线程技术可将耗时长的操作置于一个新的线程从而避免上述问题。使多CPU系统更加有效。当线程数不大于CPU数目时操作系统能够调度不同的线程执行于不同的CPU上。改善程序结构。一个既长又复杂的进程能够考虑分为多个线程成为独立或半独立的执行部分这样有利于程序的理解和维护。 【Qt中创建线程的方法】 仅仅须要子类化QThread并又一次实现它的run()函数就能够了。run()是个纯虚函数是线程运行的入口在run()里出现的代码将会在另外线程中被运行。run()函数是通过start()函数来实现调用的。 【实例】 以下一个样例给出了在应用程序中除了主线程外还提供了线程A和B。假设单击窗体中的button“Start A”Qt的控制台就会连续输出字母“A”此时button“Start A”被刷新为“Stop A”。再单击button“Start B”控制台会交替输出字母“A”和“B”。假设再单击button“Stop A”则控制台仅仅输出字母“B”。例如以下图所看到的 程序结构 thread.h代码 1 #ifndef THREAD_H2 #define THREAD_H3 4 #include QThread5 #include iostream6 7 class Thread : public QThread8 {9 Q_OBJECT
10 public:
11 Thread();
12 void setMessage(QString message);
13 void stop();
14
15 protected:
16 void run();
17 void printMessage();
18
19 private:
20 QString messageStr;
21 volatile bool stopped;
22 };
23
24 #endif // THREAD_H 注 stopped被声明为易失性变量volatile variable断电或中断时数据丢失而不可再恢复的变量类型这是由于不同的线程都须要訪问它而且我们也希望确保它能在不论什么须要的时候都保持最新读取的数值。假设省略keywordvolatile则编译器就会对这个变量的訪问进行优化可能导致不对的结果。 thread.cpp代码 1 #include thread.h2 #include QDebug3 4 Thread::Thread()5 {6 stopped false;7 }8 9 void Thread::run()
10 {
11 while(!stopped)
12 {
13 printMessage();
14 }
15 stopped false;
16 }
17
18 void Thread::stop()
19 {
20 stopped true;
21 }
22
23 void Thread::setMessage(QString message)
24 {
25 messageStr message;
26 }
27
28 void Thread::printMessage()
29 {
30 qDebug()messageStr;
31 sleep(1);
32 } 注 QTread提供了一个terminate()函数该函数能够再一个线程还在运行的时候就终止它的运行但不推荐用terminate()由于terminate()不会立马终止这个线程该线程何时终止取决于操作系统的调度策略也就是说它能够随时停止线程运行而不给这个线程自我清空的机会。更安全的方法是用stopped变量和stop()函数如样例所看到的。调用setMessage()让第一个线程每隔1秒打印字母“A”而让第二个线程每隔1秒打印字母“B”。线程会由于调用printf()而持有一个控制I/O的锁多个线程同一时候调用printf()在某些情况下回造成控制台输出堵塞而用qDebug()作为控制台输出一般不会出现上述问题。 threaddialog.h代码 1 #ifndef THREADDIALOG_H2 #define THREADDIALOG_H3 4 #include QPushButton5 #include QDialog6 #include QCloseEvent7 #include thread.h8 9 class ThreadDialog : public QDialog
10 {
11 Q_OBJECT
12
13 public:
14 ThreadDialog(QWidget *parent0);
15
16 protected:
17 void closeEvent(QCloseEvent *event);
18
19 private slots:
20 void startOrStopThreadA();
21 void startOrStopThreadB();
22 void close();
23
24 private:
25 Thread threadA;
26 Thread threadB;
27 QPushButton *threadAButton;
28 QPushButton *threadBButton;
29 QPushButton *quitButton;
30 };
31
32 #endif // THREADDIALOG_H threaddialog.cpp代码 1 #include threaddialog.h2 3 ThreadDialog::ThreadDialog(QWidget *parent) : QDialog(parent)4 {5 threadA.setMessage(A);6 threadB.setMessage(B);7 8 threadAButton new QPushButton(tr(Start A), this);9 threadAButton-setGeometry(10, 30, 80, 30);
10 threadBButton new QPushButton(tr(Start B),this);
11 threadBButton-setGeometry(110, 30, 80, 30);
12 quitButton new QPushButton(tr(Quit), this);
13 quitButton-setGeometry(210, 30, 80, 30);
14 quitButton-setDefault(true);
15
16 connect(threadAButton, SIGNAL(clicked()), this, SLOT(startOrStopThreadA()));
17 connect(threadBButton, SIGNAL(clicked()), this, SLOT(startOrStopThreadB()));
18 connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
19 }
20
21 void ThreadDialog::startOrStopThreadA()
22 {
23 if(threadA.isRunning())
24 {
25 threadAButton-setText(tr(Stop A));
26 threadA.stop();
27 threadAButton-setText(tr(Start A));
28 }
29 else
30 {
31 threadAButton-setText(tr(Start A));
32 threadA.start();
33 threadAButton-setText(tr(Stop A));
34 }
35 }
36
37 void ThreadDialog::startOrStopThreadB()
38 {
39 if(threadB.isRunning())
40 {
41 threadBButton-setText(tr(Stop B));
42 threadB.stop();
43 threadBButton-setText(tr(Strat B));
44 }
45 else
46 {
47 threadBButton-setText(tr(Start B));
48 threadB.start();
49 threadBButton-setText(tr(Stop B));
50 }
51 }
52
53 void ThreadDialog::closeEvent(QCloseEvent *event)
54 {
55 threadA.stop();
56 threadB.stop();
57 threadA.wait();
58 threadB.wait();
59 event-accept();
60 }
61
62 void ThreadDialog::close()
63 {
64 exit(0);
65 } 注 startOrStopA的逻辑是当单击A的button时假设系统推断到有线程A在执行中就把A的button刷新为“Stop A”表示能够进行stop A的动作并停止线程A的执行再将A的button刷新为“Start A”。否则假设线程A没有执行就把button刷新为表示能够执行的“Start A”启动线程A然后将Abutton刷新为“Stop A”。当不用Qt设计器时new一个button出来须要指定一个父类比方this否则执行程序窗体里没有button。new了多个button或控件须要用setGeometry来确定它们的大小和位置否则前面的被后面的覆盖终于看到的是最后一个button。setGeometry的前2个參数是相对于窗体的坐标位置后两个參数是button的长宽。单击Quit或关闭窗体就停止全部正在执行的线程而且在调用函数QCloseEvent::accept()之前等待它们全然结束这样就能够确保应用程序是以一种原始清空的状态退出的。假设没有62~65行的又一次定义close函数使进程全然退出。否则点击Quitbutton或叉号退出窗体后进程依旧驻留在系统里。 main.cpp代码 1 #include threaddialog.h2 #include QApplication3 4 int main(int argc, char *argv[])5 {6 QApplication app(argc, argv);7 ThreadDialog *threaddialog new ThreadDialog;8 threaddialog-exec();9 return app.exec();
10 } 注 在GUI程序中主线程也被称为GUI线程由于它是唯一一个同意运行GUI相关操作的线程。必须在创建一个QThread之前创建QApplication对象。转载于:https://www.cnblogs.com/bhlsheji/p/4046032.html
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/918537.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!