Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7026659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:08:48+00:00 2026-05-28T00:08:48+00:00

I have a server which handles multiple incoming socket connections and creates 2 different

  • 0

I have a server which handles multiple incoming socket connections and creates 2 different threads which store the data in XML format.

I was using the lock statement for thread safety almost in every event handler called asyncronously and in the 2 threads in different parts of code. Sadly using this approach my application significantly slows down.

I tried to not use lock at all and the server is very fast in execution, even the file storage seems to boost; but the program crashes for reasons I don’t understand after 30sec – 1min. of work.

So. I thought that the best way is to use less locks or to use it only there where’s strictly necessary. As such, I have 2 questions:

  1. Is the lock needed when I write to the public accessed variables (C# lists) only or even when I read from them ?

  2. Is the lock needed only in the asyncronous threads created by the socket handler or in other places too ?

Someone could give me some practical guidelines, about how to operate. I’ll not post the whole code this time. It hasn’t sense to post about 2500 lines of code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:08:49+00:00Added an answer on May 28, 2026 at 12:08 am

    Is the lock needed when I write to the public accessed variables (C# lists) only or even when I read from them ?

    Yes (even when you read).

    Is the lock needed only in the asyncronous threads created by the socket handler or in other places too ?

    Yes. Wherever code accesses a section of code which is shared, always lock.


    This sounds like you may not be locking individual objects, but locking one thing for all lock situations.

    If so put in smart discrete locks by creating individual unique objects which relate and lock only certain sections at a time, which don’t interfere with other threads in other sections.

    Here is an example:

    // This class simulates the use of two different thread safe resources and how to lock them
    // for thread safety but not block other threads getting different resources.
    public class SmartLocking
    {
        private string StrResource1 { get; set; }
        private string StrResource2 { get; set; }
    
        private object _Lock1 = new object();
        private object _Lock2 = new object();
    
        public void DoWorkOn1( string change )
        {
            lock (_Lock1)
            {
                _Resource1 = change;
            }
        }
    
        public void DoWorkOn2( string change2 )
        {
            lock (_Lock2)
            {
                _Resource2 = change2;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a thread which handles all the HTTP Connections to the server and
I have a remote server which handles various different commands, one of which is
we have this scenario: A server which contains needed data and client component which
I am creating a servlet(proxy server) which handles multiple request from the client.I do
I have a ressource manager that handles multiple TCP-Connections. These connections are pthreads. How
Is that possible to have a single PHP SOAP server which will handle requests
I have an HTTP server which is in our internal network and accessible only
I have a web server which saves cache files and keeps them for 7
I have an x64 server which, since my libraries are compiled to AnyCPU, run
We have a reporting server which has a bunch of reports and some ad

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.