C + + Promotion of multi-threading five of the secret multi-threaded (if possible, Microsoft Office drinking coffee)

Source: Internet
Author: User

Multithreading Front has written four articles, I also through a little closer to two weeks of time on multi-threaded now is not afraid of, this article is on my previous multithreading some of the problems of the supplement, correct

1. Creatthread () This is an operating system-provided, encapsulated, thread-creation function, which I previously thought was the function of creating a thread, the real function of this function is to submit the application for creating threads to the operating system (some people say I'm keying, I don't think so), Because this function is encapsulated in the operating system, the internal implementation is dark for us, so when we submit the application after the operating system how to create a thread we do not know, the time we need to create a good thread we do not know, so that the first article mentioned in the multithreading of a feature, create unpredictable, The creation of a good time is unpredictable, not that it will not create unpredictable

2. I also checked some information, said multithreading is very troublesome, because of its unpredictable cause sometimes have to be careful to correct, after my study of this period of time I think the multi-threaded error is mainly focused on a problem (the function of creating a thread of the name of the missing letters of these do not count OH), is Multi-threaded contention for shared resources , or the timing of multithreaded operations

Let me give you an example:

Premise: The parent class constructs a function that submits the creation thread to the operating system, the subclass has a member pointer, and the child class constructs the pointer open space to accept a string of strings, the thread callback function calls the function of the subclass, and the function is the function of printing the child class member pointer in the string, building the sub-class object

What's the problem? I found it very soon after--------------thought, so let's take a look at it.

Idea: Main Line Chengjianzi class object

The main thread jumps out of the function body of the subclass construct

The main thread jumps to the parent class construct function body, the main thread submits the CREATE thread request

(The problem here) if the operating system this person is very slow, do not know when to create a child thread, the main thread is synchronous running, the main thread jumps to the sub-class construct to give its own member pointer Open space, (the problem comes) if the previous operating system this person quickly, and soon created a child thread, While the child thread calls the function of the subclass, when called the main thread has not yet given the member pointer of the subclass to open up space, when the child thread printing error is not initialized

--in other cases, when a child thread calls a subclass function, the main thread opens up space for the child class member pointer, but without assigning a value to the pointer, printing can be messy.

--in other cases, if the OS guy is still dozing when he sees my creation thread application, my main thread has opened up a space assignment for the subclass member pointer, and the child thread prints no problem.

3. The above ideas in my point of view is more clear what is called shared resources, before I think that a few sub-threads can rob the resources used, these resources are only in the main thread in the global, the main thread is not used, I ignored the main thread is also threads Ah, In the example above, the member pointer of this subclass is a shared resource!

4. In 2, we also realize that a problem has not been found, I am in the process of thinking, I always emphasize the main thread or sub-thread , because I just out of the nature of the problem occurs because of the problem of multithreading, multithreading should mention which thread, In the future when I went to the interview HR asked me similar questions, I will give him clearly clear

5. Later multithreading may be complex, but I do not feel afraid at the moment (perhaps not see dictionary), I now feel what the thread is, is not a function , create how to create I go to the tube? I'd like to take care of it, if I may, I guess I can sit at Microsoft's office building and have coffee.

(After writing this article is very happy, multi-threading, socket, database is basic, finally solved a mountain, the next time to continue to refuel, to Mr. Sun to see)

(PanPen120 original, if there are suggestions, please leave a message)

C + + Promotion of multi-threading five of the secret multi-threaded (if possible, Microsoft Office drinking coffee)

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.