-1

I used to play with Visual Basic 5.0 as a kid. It allowed me to place lots of 'timers' on a 'form' that would seemingly run simultaneously... whereas now I'm starting to learn lower-level programming languages and everything seems to run one-thing-at-a-time.

Can someone help my mind grasp this concept of simultaneity and why VB seemed to have it easily available but learning c++ so far I've not met anything that feels like I can replicate that simultaneous running of code?

Is most of the 'simultaneity' in simple Visual Basic programs actually an illusion that c++ code can easily recreate? Sorry for lacking the terminology.

edit: Thanks for the replies. They have clarified that it was indeed usually an illusion of simultaneity. To explain further what was in my mind, at my early stage in learning c++, I don't think I know how to write a program that, every 2 seconds, will increment the value of 'x'... while simultaneously every 5 seconds, incrementing 'y'.

  • Are you talking about multithreading? If so have a look at this: https://stackoverflow.com/questions/266168/simple-example-of-threading-in-c – Rakete1111 Apr 07 '19 at 17:42
  • In all likelihood you can get actual 'simultaneity' in C++ that you couldn't in Visual Basic. It definitely was an illusion. There is a deep world here that you've just asked a simple vague question about. I don't think you can get an answer here on Stack Overflow because people here tend to be focused on really specific problems with definite clearly correct answers. – Omnifarious Apr 07 '19 at 17:46
  • It's only in the past 5-7 years that it's been the common case that processors could do more than one thing at a time. (Multiple cores). Before, it was all an illusion created by the computer switching between things to do at just the right moment so that you didn't notice. In C++ you have access to something called 'multi-threading' through the `` header (and other things) that allows you to actually do several things at once. Using it is tricky. I don't think Visual Basic ever allowed this kind of thing, but I don't know it well enough to say for sure. – Omnifarious Apr 07 '19 at 17:49
  • 1
    You want to read about the standard C++ [Thread support](https://en.cppreference.com/w/cpp/thread) and [Date and time](https://en.cppreference.com/w/cpp/chrono) libraries. – πάντα ῥεῖ Apr 07 '19 at 17:49
  • 2
    Even in single-threaded you can do things asynchronously (with emulation) using event loops. If you're doing GUI programming, you already have that in your program (all window, button, etc. events that you handle are dispatched by a single event-loop running somewhere behind the scenes). – Some programmer dude Apr 07 '19 at 17:55

1 Answers1

0

This question isn't appropriate for Stack Overflow, but I really like answering people's questions, so I will anyway, even though the answer will be necessarily incomplete.

What Visual Basic was doing was providing you with a special environment in which things appeared to be running simultaneously. All those timers were just entries in a data structure that told Visual Basic when it had to go act like that timer had just expired. It used the current time to decide when it should run them.

In general, Visual Basic is what controlled the flow of your program. In between all the statements you wrote, the Visual Basic runtime was doing all kinds of things behind the scenes for you. Things like checking up on the data structures it was keeping of which timer to fire when and so on.

There are two main ways of controlling the flow of your program in such a way as to make it seem like several things are happening at the same time. What Visual Basic was doing is called an 'event loop'. That's a main loop in your program that keeps track of all the things that need doing and goes and runs the code that does them at the right times. When someone clicks on a window, your program gets an event, the event loop sees that event and runs the 'clicked on a window' code when it gets control. It sort of seems like your program just responds instantly to the click, but that's not what's really happening.

The other way is called 'multi-threading'. In that way your program may really do several things at the same time. The operating system itself decides how to allocate CPU resources to running 'threads'. If there is a lot of competition for CPU resources, the operating system may start running one program (or thread) for a little while (a thousandth of a second or so) and then switch to running a different program (aka process (threads and processes are strongly related, but definitely distinct concepts)). The switching happens so fast, and can happen at any instant, so it seems like several things are happening at once. But, if there are enough CPU resources available (multiple cores) the computer can actually just run several things at the same time.

There are advantages and disadvantages to each approach. Actually dealing with two things modifying the same data at the exact same time and making sure everything stays consistent can be very hard, and is usually accomplished by having sections of your program in only one thread is allowed to be running at a time so that those modifications can't actually happen in the same instant and they are 'serialized' so that one thread's modification happens before another. These are usually called 'critical sections' and access to these critical sections is typically controlled by something called a 'mutex' (aka mutual exclusion) lock.

Event driven code doesn't have that problem since typically one event must be fully processed before the code to process the next event can start. But this can lead to under-utilization of CPU resources, and it can also sometimes introduce noticeable delays in handling events as one event can't be processed until the code to process the preceding event is finished.

In short, Visual Basic was making event driven programming really easy and clean to use, and providing you with the illusion that several things were running at the same time.

These, in general, are fairly deep computer science topics, and people get their PhDs studying some of this stuff. But a working understanding of exactly how event driven code and how multi-threading code works isn't that hard.

There are also other models of concurrency out there, and hybrid models like 'co-routines' that look like threads but are really events.

I put all the most useful concept handles in quotes to emphasize them.

Omnifarious
  • 54,333
  • 19
  • 131
  • 194