Threads and applications
Threading is an important part of writing applications.
Before you start writing the code, you must think twice on threading. First you have to identify the tasks that can be done by threads. Second you think or innovate a way to communicate between threads. Finally you start writing the code. For example an application receives some data on the serial port, computer the data and than plots some graphical means. For this application to run properly, you will have to create 3 threads: - one for communication (working thread) - one for computation (working thread) - one for user interface (user thread) Now that you have created the 3 threads, it is time for you to start thinking of communication part. The first thread implements serial communication and it will transmit to the second thread (the computational one) the received and unpacked data. The second thread will do the computation and will send the results to the user-interface thread that will plot the data. Imagine that no thread had been created. The computation will be something like 10 exp 30. This will slow down or even kill your application. Or imagine that some problems may occur with the serial (e.g. waiting several seconds to receive some data that will not show up). This will also block your application. Now that I have presented you some reasons to use threads, we shall move on to “how to create a thread”.
How to create a thread
Right click the class view folder that contains your program and choose new class.
Choose the Base class to be CWinThread and name your class CThreadEx1.
Hitting OK will bring you to the next configuration in your ClassView.
The class have now the constructor, the destructor and two methods: InitInstance() and ExitInstance(). In the InitInstance do the initialization and in the ExitInstance do the cleanup. NOTE! The Constructor is protected! Move the constructor to the public zone, this way you can create thread objects in the OnCreate method of your application.
How to use a thread
You will add two more methods: OnIdle (use the ClassWizard to add this) an IsIdleMessage. The last one you will insert manually. In the header define virtual BOOL IsIdleMessage(MSG* pMsg); and the public overrides will look like this:
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CThreadEx1)
public:
virtual BOOL InitInstance();
virtual int ExitInstance();
virtual BOOL OnIdle(LONG lCount);
virtual BOOL IsIdleMessage(MSG* pMsg);
//
}}
AFX_VIRTUAL
This will be the body of IsIdleMessage for the moment:
BOOL CThreadEx1::IsIdleMessage(MSG* pMsg)
{
return CWinThread::IsIdleMessage(pMsg);
}
The communication to a thread it is done by messages. The thread receives messages in a queue and pops the messages one by one. The messages are then unpacked, understood, acknowledged and, of course, executed. When is ready to be processed, every message in the queue will be extracted and this event will occur in IsIdleMessage. Here you will have a pMsg pointer to a MSG structure that contains your message. When no messages are in the queue, OnIdle will occur. There are two type of messages that you could send to a thread: blocking and unblocking. The blocking messages type are posted using the SendThreadMessage(…). This means that when you use this function the execution of your program will stop at the SendThreadMessage() until the thread will execute the message. The unblocking messages type are posted using PostThreadMessage(…). The message will be posted to the queue and the execution of the program will continue asynchronous.
Example
To better understand threading I will demonstrate the functionality in a simple example. One simple dialog (a dialog-based project) will multiply some matrix. If you will multiply the matrix, let’s say when have clicked the Start button, the application will slow down until this job is finished. That’s the reason I implemented one thread to do the multiplying. Let me explain what this example does before we’ll get down to the code.
Attachments
Source Files thread vc++
Project Files thread vc++