Lesson15-QT multithreading _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Lesson15-QT multithreading. Lesson15-QT multithreading 1, what is thread 1, thread process: a program is being executed, it is the smallest unit of the thread of resource allocation: The smallest unit of the process of program execution appears a lot of lesson15-QT multithreading
1. what is a thread?
1. thread
Process: A program being executed. it is the smallest unit of resource allocation.
Thread: minimum unit of program execution

Process has many drawbacks. first, because the process is the resource owner, there is a large space-time overhead for creation, revocation, and switching. Therefore, lightweight processes need to be introduced; second, due to the emergence of symmetric multi-processor (SMP), multiple operating units can be satisfied, while the parallel overhead of multiple processes is too large.

2. thread terminology
Concurrency means that at the same time, only one command can be executed, but multiple process commands are quickly rotated for execution, so that multiple processes can be executed simultaneously at a macro level.
It seems to happen at the same time

Parallelism refers to the simultaneous execution of multiple commands on multiple processors at the same time.
Real happening at the same time

Synchronization: calls with dependency between each other should not "occur at the same time", but synchronization is to prevent "happening at the same time ".
The concept of asynchronization is relative to synchronization. any two operations that are independent of each other are asynchronous, which indicates that things happen independently.

3. thread advantages
1) concurrency of developing programs in multi-processor
2) while waiting for slow IO operations, the program can execute other operations to improve concurrency
3) modular programming, which can clearly express the relationship between independent events in the program and clear the structure
4) a small amount of system resources are occupied.

Multi-thread does not have to be multi-processor
In GUI programs, multithreading is often used. one thread is used to respond to the interface, and other threads can process lengthy operations in the background.
Qt's object meta-system supports communication between objects in different threads using signals and slots.




II. QT multithreading
Multithreading in Qt is very simple. as long as the sub-class is QThread, there is a protected type run function in QThread. rewrite the run function to implement multithreading.
1. QT thread
Multithreading in Qt is very simple. as long as QThread is subclass and the run function is rewritten, multithreading can be implemented.

Class MyThread: public Thread
{
Public:
MyThread ();
Protected:
Void run ();
Private:
Volatile boolean stopped;
}

The run function is started by the thread's start method. The thread also has the isRunning method to determine whether the thread is running. The terminate method ends the thread.


2. semaphores for thread synchronization
Semaphores make the thread do not need to wait busy, is an extension of mutex. Using semaphores ensures that two key codes are not concurrent. When entering a piece of key code, the thread must obtain the Semaphore and release it when exiting. Semaphores can be accessed by multiple threads at the same time.

QSemaphore class of Qt semaphores:
Acquire () is used to obtain resources, and free () is used to release resources.

For producer and consumer examples, make sure that there is sufficient space during producer production and that there are resources in the space during consumer consumption.
QSemaphore freeByte (100) production has 100 space
QSemaphore useByte (0) the consumer has no resources
Producer
{
FreeByte. acquire ()
Byte = n
UseByte. release ()
}
Consumer
{
UseByte. acquire ()
Printf byte
FreeByte. release ()
}


3. conditional variables for thread synchronization
QWaitCondition allows the thread to wake up other threads under certain conditions. This can also be because the thread does not have to wait busy. the condition variable must be used together with the mutex.

QMutex mutex; QWaitCondition condition;
Condition. wait (& mutex)
Condition. wakeAll ()
The wait function unlocks the mutex and waits for it to return.
The wakeAll function will wake up all threads waiting for the mutex.


4. thread priority
The actual task may run a thread first, so the thread priority needs to be set.
The setPriority function can set the priority of a thread, or input the priority of a thread in the start function when the thread starts.


III. instances
1. multithreading
 
 
  1. #ifndef MYTHREAD_H
  2. #define MYTHREAD_H

  3. #include

  4. class MyThread : public QThread
  5. {
  6. Q_OBJECT
  7. public:
  8. MyThread();
  9. void stop();
  10. volatile bool stopped;
  11. protected:
  12. void run();
  13. };
  14. #endif
 
 
  1. #include "myThread.h"
  2. #include

  3. MyThread::MyThread()
  4. {
  5. stopped = false;
  6. }

  7. void MyThread::run()
  8. {
  9. int i=0;
  10. while(!stopped)
  11. {
  12. qDebug()<<"thread id:"
  13. i++;
  14. sleep(2);
  15. }
  16. stopped = false;
  17. }

  18. void MyThread::stop()
  19. {
  20. stopped = true;
  21. }

2. semaphores

 
 
  1. #ifndef PRODUCER_H
  2. #define PRODUCER_H

  3. #include

  4. class Producer : public QThread
  5. {
  6. Q_OBJECT
  7. public:
  8. Producer();
  9. protected:
  10. void run();
  11. };

  12. #endif

 
 
  1. #ifndef CONSUMER_H
  2. #define CONSUMER_H

  3. #include

  4. class Consumer : public QThread
  5. {
  6. Q_OBJECT
  7. public:
  8. Consumer();
  9. protected:
  10. void run();
  11. };

  12. #endif

 
 
  1. #include "producer.h"
  2. #include "consumer.h"
  3. #include
  4. #include

  5. #define SIZE 50
  6. QSemaphore freeByte(SIZE);
  7. QSemaphore useByte(0);

  8. Producer::Producer()
  9. {

  10. }
  11. void Producer::run()
  12. {
  13. for(int i=0; i
  14. {
  15. freeByte.acquire();
  16. qDebug()<<"produer:"<
  17. useByte.release();
  18. sleep(1);
  19. }
  20. }
  21. Consumer::Consumer()
  22. {

  23. }
  24. void Consumer::run()
  25. {
  26. for(int i=0; i
  27. {
  28. useByte.acquire();
  29. qDebug()<<"consumer:"<
  30. freeByte.release();
  31. sleep(2);
  32. }
  33. }


3. condition variables

 
 
  1. #ifndef THREAD_H
  2. #define THREAD_H

  3. #include

  4. class Producer : public QThread
  5. {
  6. Q_OBJECT
  7. public:
  8. Producer();
  9. protected:
  10. void run();
  11. };

  12. class Consumer : public QThread
  13. {
  14. Q_OBJECT
  15. public:
  16. Consumer();
  17. protected:
  18. void run();
  19. };

  20. #endif

 
 
  1. #include "thread.h"
  2. #include
  3. #include
  4. #include

  5. QMutex mutex;
  6. QWaitCondition empty, full;
  7. int num=0;
  8. int buffer[50];
  9. int useByte=0;


  10. Producer::Producer()
  11. {

  12. }
  13. void Producer::run()
  14. {
  15. for(int i=0; i<50; i++)
  16. {
  17. mutex.lock();
  18. if(useByte==50)
  19. empty.wait(&mutex);
  20. num++;
  21. buffer[i] = num;
  22. qDebug()<<"producer:"<
  23. useByte++;
  24. full.wakeAll();
  25. mutex.unlock();
  26. sleep(1);
  27. }
  28. }
  29. Consumer::Consumer()
  30. {

  31. }
  32. void Consumer::run()
  33. {
  34. for(int i=0; i<50; i++)
  35. {
  36. mutex.lock();
  37. if(useByte==0)
  38. full.wait(&mutex);
  39. qDebug()<<"consumer"<
  40. useByte--;
  41. empty.wakeAll();
  42. mutex.unlock();
  43. sleep(2);
  44. }
  45. }



Thread 1, what is thread 1, thread process: a program being executed, it is the smallest unit thread for resource allocation: The smallest unit process for program execution appears very...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.