none
Simple questions on threads RRS feed

  • Question

  • If I have a class that is instantiated 1000 times, and it has this code in one of the methods.

    new Task(Process, httpContext).Start();

    Are all one thousand instances thread safe for this line, since they are different instances?  (I am sure that no single class will get called more than one at a time, however the different instances will in fact call at the same time.) If it is not thread safe, I would like more info, that I could cite.

    Thanks.





    • Edited by danielsn Thursday, December 28, 2017 12:40 AM
    Thursday, December 28, 2017 12:04 AM

Answers

  • Hello danielsn,

    >>Are all one thousand instances thread safe for this line, since they are different instances?

    The thread safe is for the thread rather than instance. Even if you have one thousand instances you should pay attentions how many threads are running and what are the threads doing. In you code the thread pool will allocate one thread to invoke the Process method. Maybe the large number of instance called the above code at the same time. it will generate a large number of threads to run this process method simultaneously. At this moment you must consider the thread safe issues.

    So in your situations the thread safe are related to the Process method. In general local variables cannot be accessed between threads so they are thread safe. But some fields or sharing date could be changed among threads so they are not thread safe. .Net has provided a lot of ways to achieve thread safe. Like using Lock statement, volatile key word, Semaphore class and so on. I think the thread safe is a very complex issue rather than simple question.

    And this link will give you a better understanding.

    https://stackoverflow.com/questions/9848067/what-makes-a-method-thread-safe-what-are-the-rules

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Fei HuModerator Thursday, December 28, 2017 7:52 AM
    • Marked as answer by danielsn Wednesday, March 28, 2018 10:55 PM
    Thursday, December 28, 2017 7:49 AM
    Moderator

All replies

  • A class can be defined only once; there cannot be more than one definition of a class. You say "no single class will get called more than one at a time" and I am saying that that is impossible. Probably what you meant to say is that "no instance of a class will be called more than once". You might feel that I am being picky about terminology but sometimes the use of the wrong terminology can cause problems.

    Do not create 1000 tasks! That is unnecessary and very inefficient.

    When there are only one processor core it is true that true parallel processing is not possible but tasks/threads are designed so that one can wait for something while others execute. In that manner it is totally possible for more than one task to access something at the same time. And now that most computers, even the little ones that are in many people's pockets and purses, have multiple cores, it is especially possible for more than one task to access the same object.



    Sam Hobbs
    SimpleSamples.Info



    Thursday, December 28, 2017 3:01 AM
  • Hello danielsn,

    >>Are all one thousand instances thread safe for this line, since they are different instances?

    The thread safe is for the thread rather than instance. Even if you have one thousand instances you should pay attentions how many threads are running and what are the threads doing. In you code the thread pool will allocate one thread to invoke the Process method. Maybe the large number of instance called the above code at the same time. it will generate a large number of threads to run this process method simultaneously. At this moment you must consider the thread safe issues.

    So in your situations the thread safe are related to the Process method. In general local variables cannot be accessed between threads so they are thread safe. But some fields or sharing date could be changed among threads so they are not thread safe. .Net has provided a lot of ways to achieve thread safe. Like using Lock statement, volatile key word, Semaphore class and so on. I think the thread safe is a very complex issue rather than simple question.

    And this link will give you a better understanding.

    https://stackoverflow.com/questions/9848067/what-makes-a-method-thread-safe-what-are-the-rules

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Fei HuModerator Thursday, December 28, 2017 7:52 AM
    • Marked as answer by danielsn Wednesday, March 28, 2018 10:55 PM
    Thursday, December 28, 2017 7:49 AM
    Moderator