Excerpt-multithreaded programming on the Pocket PC with Visual C ++

Source: Internet
Author: User
The modular drive design of learning is designed, haha.

While there have been several resources written to cover creating threads using Microsoft Win32 C functions, there is a temporary age of information on how to handle threads in C ++. since C ++ programmers can use C functions in their programs, threading is typically done in C, at the cost of some functionality and the Object Modeling available to applications written in C ++. by threading with the methods described in this article, you can invoke any member function of a C ++ class in a separate thread.

Applies:
Microsoft Windows powered Pocket PC 2000
Microsoft Embedded Visual C ++ 3.0

Gotchas

None

Supported ages supported

English

Planning your application

Application Design is an article (or a book) in itself, but suffice it to say that your application development will go much more smoothly if you have a plan to follow. most likely, the best way to plan the threads in your application is either on paper or on a whiteboard.

For each task that will take longer than a fraction of a second, consider making it a thread, since arbitrary Ming any task in the same thread as the user interface will cause the user interface to "freeze" while that task is completed MED. there are plenty of applications out there that do everything in the same thread as the user interface, but in general the development community shocould move away from this type of programming, since it tends to make applications feel sluggish and unresponsive.

Creating classes with thread Methods

Starting a thread is only a little more complex than calling a function. by callingCreatethread ()OrAfxbeginthread ()Functions, and passing the address of your own function, Microsoft Windows will call your function in a separate thread.AfxbeginthreadIs used for MFC (Microsoft Foundation Classes) applications, and it calltheCreatethreadFunction,

Typically, a C ++ programmer who is familiar with multithreading in C will try to pass a member function of their class toCreatethread ()OrAfxbeginthread ()Functions. Unfortunately, C ++ member functions don't have a specific memory address.

Ah, you might be thinking, but what about static methods? Yes, static methods do have a fixed address, and can in fact be passedCreatethread ()OrAfxbeginthread ()Successfully. unfortunately, static methods do not relate to any specific instance of a class, so they can't access any data members of the class that aren't also static (making them pretty useless as a thread function ).

Fortunately, there's a very good solution to the problem:

  1. Create a nonstatic public method for the thread in your class. This method will take no parameters, and will return a DWORD.
  2. Create a static protected method for the thread with a similar name. I suggest adding the suffix "_ thread" so that if your nonstatic method was calledElastic msearch, "The corresponding static method wocould be calledPerformsearch_thread. This method will take one parameter, a pointer to its own object class, and will return a DWORD.
  3. In the static method, call the nonstatic method of the Object Pointer passed in as a parameter. For example, if the parameter is calledPthis, CallPthis-> elastic msearch (). This step is very important, because by calling back into a nonstatic method, the thread is then granted access to the private and protected data members of the class.
  4. Define a static, manual reset event handle calledM_heventterminate. This handle will be used to signal the threads that they shocould terminate.
  5. Define an array of thread handles for each thread the application will be starting. Using an array now will make it easier to handle application termination later.
  6. Spawn the thread from inside the class. Do this by callingCreatethread ()OrAfxbeginthread ()And pass the static method, and the object'sThisPointer as the parameter. Typically, spawn the threads either in an initialization method, often called from the class constructor.

Figure 1: the majority of your class methods can execute in one thread, and specific methods can be run in another thread.

A simple example of the process is available for download at the virtual office systems web site.

Conclusion

There is certainly more to good threading techniques than can be covered in a brief article like this one. as a developer, you shoshould strive to use the tools at your disposal, and "C style" multithreading can now be left in C, where it belongs.

Related Article

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.