The C++ programming language itself does not provide threading – that’s provided (optionally) by your operating system via a library. O/Ss like Windows and Linux provide threads; DOS was an operating system that did not.
The concept is that within a single application, you have independent pieces of code that execute “simultaneously.”
Imagine you are running on a typical multitasking operating system like Windows or Linux, where you might have many applications running at once – a web browser, editor, desktop manager, etc. The operating system takes care of giving each application the resources it needs to run.
A threaded application is the same concept – only the separate pieces, rather than being individual applications, are part of a single program.
You can use data structures to communicate between these threads – data (variables) or messages (again, this is all provided by a library your O/S has to provide).
Typically a thread has to be started with a call to a library function that requires a pointer to a static function. This means you can’t use C++ methods unless they are declared static.
Here’s an example from MS Windows – let’s say I want to write a terminal program like Hyperterm. I want to allow the user to select items off the menus, but at the same time I have to listen to the serial port for characters coming in. If I made a blocking call on the port read function from the main application code, the whole application would appear to be frozen until a character actually came in.
What I have to do instead is to create a thread to listen on the serial port, then the main application can still respond to Windows events like the user selecting from a menu. The thread can then alert the main message loop that a character has arrived, and I can pick up that event and process the character. So to the user, a character arriving or not arriving is like any other event – mouse movement, click, or whatever.
The C++ programming language itself does not provide threading – that’s provided (optionally) by your operating system via a library. O/Ss like Windows and Linux provide threads; DOS was an operating system that did not.
The concept is that within a single application, you have independent pieces of code that execute “simultaneously.”
Imagine you are running on a typical multitasking operating system like Windows or Linux, where you might have many applications running at once – a web browser, editor, desktop manager, etc. The operating system takes care of giving each application the resources it needs to run.
A threaded application is the same concept – only the separate pieces, rather than being individual applications, are part of a single program.
You can use data structures to communicate between these threads – data (variables) or messages (again, this is all provided by a library your O/S has to provide).
Typically a thread has to be started with a call to a library function that requires a pointer to a static function. This means you can’t use C++ methods unless they are declared static.
Here’s an example from MS Windows – let’s say I want to write a terminal program like Hyperterm. I want to allow the user to select items off the menus, but at the same time I have to listen to the serial port for characters coming in. If I made a blocking call on the port read function from the main application code, the whole application would appear to be frozen until a character actually came in.
What I have to do instead is to create a thread to listen on the serial port, then the main application can still respond to Windows events like the user selecting from a menu. The thread can then alert the main message loop that a character has arrived, and I can pick up that event and process the character. So to the user, a character arriving or not arriving is like any other event – mouse movement, click, or whatever.