Multithreading Tutorial - Code. Project. This article demonstrates how to write a multithreaded Windows program in C++ using only the Win. CAS Modifies Its Proposed CERA. Francis Miller of the ERM Committee urging that the CAS devise its own ERM. Android development has its own. It is the only language I know which compiles to Java source code, which is its most. Port Chester has increased its permit-amnesty program in an effort to provide incentives for people to bring their homes up to code. The amnesty program was initially established in 2012 to provide relief to. API. Background. When you run two programs on an Operating System that offers memory protection, as Windows and UNIX/Linux do, the two programs are executed as separate processes, which means they are given separate address spaces. This means that when program #1 modifies the address 0x. A 1. 23. 4 in its memory space, program #2 does not see any change in the contents of its memory at address 0x. A 1. 23. 4. With simpler Operating Systems that cannot accomplish this separation of processes, a faulty program can bring down not only itself but other programs running on that computer (including the Operating System itself). The ability to execute more than one process at a time is known as multi- processing. A process consists of a program (usually called the application) whose statements are performed in an independent memory area. There is a program counter that remembers which statement should be executed next, and there is a stack which holds the arguments passed to functions as well as the variables local to functions, and there is a heap which holds the remaining memory requirements of the program. The heap is used for the memory allocations that must persist longer than the lifetime of a single function. In the C language, you use malloc to acquire memory from the heap, and in C++, you use the new keyword. Sometimes, it is useful to arrange for two or more processes to work together to accomplish one goal. One situation where this is beneficial is where the computer's hardware offers multiple processors.
In the old days this meant two sockets on the motherboard, each populated with an expensive Xeon chip. Thanks to advances in VLSI integration, these two processor chips can now fit in a single package. Examples are Intel's . If you want to keep two microprocessors busy working on a single goal, you basically have two choices: design your program to use multiple processes (which usually means multiple programs), ordesign your program to use multiple threads. So, what's a thread? A thread is another mechanism for splitting the workload into separate execution streams. A thread is lighter weight than a process. This means, it offers less flexibility than a full blown process, but can be initiated faster because there is less for the Operating System to set up. The separate address space is what is missing. When a program consists of two or more threads, all the threads share a single memory space. If one thread modifies the contents of the address 0x. A 1. 23. 4, then all the other threads immediately see a change in the contents of their address 0x. A 1. 23. 4. Furthermore, all the threads share a single heap. If one thread allocates (via malloc or new) all of the memory available in the heap, then attempts at additional allocations by the other threads will fail. But each thread is given its own stack. This means, thread #1 can be calling Function. Which. Computes. ALot() at the same time that thread #2 is calling Function. Which. Draws. On. The. Screen(). Both of these functions were written in the same program. There is only one program. But, there are independent threads of execution running through that program. What's the advantage? Well, if your computer's hardware offers two processors, then two threads can run simultaneously. And even on a uni- processor, multi- threading can offer an advantage. Most programs can't perform very many statements before they need to access the hard disk. This is a very slow operation, and hence the Operating System puts the program to sleep during the wait. In fact, the Operating System assigns the computer's hardware resources to somebody else's program during the wait. But, if you have written a multi- threaded program, then when one of your threads stalls, your other threads can continue. The Jaeschke Magazine Articles. One good way to learn any new programming concept is to study other people's code. You can find source code in magazine articles, and posted on the Internet at sites such as Code. Project. I came across some good examples of multi- threaded programs in two articles written for the C/C++ Users Journal, by Rex Jaeschke. In the October 2. Jaeschke wrote an article entitled . Unfortunately, the C/C++ Users Journal magazine folded shortly after these articles appeared. But, the original articles and Jaeschke's source code are still available at the following websites: You'll notice that the content from the defunct C/C++ Users Journal has been integrated into the Dr. Dobb's Portal website, which is associated with Dr. Dobb's Journal, another excellent programming magazine. You might not be familiar with the notation C++/CLI. You're probably familiar with Java and C#, which are two languages that offer managed code where the Operating System rather than the programmer is responsible for deallocating all memory allocations made from the heap. C++/CLI is Microsoft's proposal to add managed code to the C++ language. I am not a fan of this approach, so I wasn't very interested in Jaeschke's original source code. I am sure Java and C# are going to hang around, but C++/CLI attempts to add so many new notations (and concepts) on top of C++, which is already a very complicated language, that I think this language will disappear. But, I still read the original C/C++ Users Journal article and thought Jaeschke had selected good examples of multi- threading. I especially liked how his example programs were short and yet displayed data corruption when run without the synchronization methods that are required for successful communication between threads. So, I sat down and rewrote his programs in standard C++. This is what I am sharing with you now. The source code I present could also be written in standard C. In fact, that's easier than accomplishing it in C++ for a reason we will get to in just a minute. This is probably the right time to read Jaeschke's original articles, since I don't plan to repeat his great explanations of multitasking, reentrancy, atomicity, etc. For example, I don't plan to explain how a program is given its first thread automatically and all additional threads must be created by explicit actions by the program (oops). The URLs where you can find Jaeschke's two articles are given above. Creating Threads Under Windows. It is unfortunate that the C++ language didn't standardize the method for creating threads. Therefore, various compiler vendors invented their own solutions. If you are writing a program to run under Windows, then you will want to use the Win. API to create your threads. This is what I will demonstrate. The Win. 32 API offers the following function to create a new thread: uintptr. The first is the name of the function which you want the new thread to begin executing. This is called the thread's entry- point- function. You get to write this function, and the only requirements are that it take a single passed parameter (of type void*) and that it returns nothing. That is what is meant by the function signature: void( . However, I always set this parameter to 0, which forces the Windows Operating System to select the stack size for me, and I haven't had any problems with this approach. The final passed parameter to the . This will be made clear by the following example program: #include < span class=. Simply request a Win. Console Program from Visual C++ . NET 2. 00. 3's New Project Wizard and then . I am providing Visual C++ . NET 2. 00. 3 workspaces for Jaeschke's (modified) programs, but you need to know the key to starting a multi- threaded program from scratch: you must remember to perform one modification to the default project properties that the New Project Wizard gives you. Namely, you must open up the Project Properties dialog (select . In the left hand column of this dialog, you will see a tree view control named . In the right hand area of the Project Properties dialog, you will now see listed . If you forget to do this, your program won't compile, and the error message will complain about the . Without the Sleep() statement, the program's output will probably only show a single call to the silly() function, with the passed argument - 5. This is because the program's process terminates as soon as the main thread reaches the end of the main() function, and this may occur before the Operating System has had the opportunity to create the other thread for this process. This is one of the discrepancies from what Jaeschke says concerning C++/CLI. Evidently, in C++/CLI, each thread has an independent lifetime, and the overall process (which is the container for all the threads) persists until the last thread has decided to die. Not so for straight C++ Win. The death of this thread means the death of all the other threads. Using a C++ Member Function as the Thread's Entry- Point- Function. The example program I just listed really isn't a C++ program because it doesn't use any classes. It is just a C language program. The Win. 32 API was really designed for the C language, and when you employ it with C++ programs, you sometimes run into difficulties. Such as this difficulty: . Every C++ member function has a hidden first passed parameter known as the this parameter. Via the this parameter, the function knows which instance of the class to operate upon. Because you never see these this parameters, it is easy to forget they exist. Now, let's again consider the . This entry- point- function must accept a single void* passed param. The function signature required by . So, we use a two- step procedure to accomplish our goal: we ask . The static class function knows to convert the void* parameter to a pointer of a class instance. We now know which instance of the class should call the real entry- point- function, and this call completes the two step process. The relevant code (from Jaeschke's modified Part 1 Listing 1 program) is shown below: class Thread. X. . This is typical of Microsoft's Win.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
December 2016
Categories |