socket programming and thread safety

This is a discussion on socket programming and thread safety within the Networking/Device Communication forums, part of the General Programming Boards category; I am looking at programming an application that uses sockets. My concern is with thread safety as I have never ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    100

    socket programming and thread safety

    I am looking at programming an application that uses sockets. My concern is with thread safety as I have never used threading before except with gui worker threads. I want the server application to service multiple clients on separate threads. As I have not started programming the server yet I was hoping you will be able to aid me in my design by telling me what I need to know to make my server thread safe.

    Also if I had a class called X with a member function called y and I created two objects of this class on separate threads. Can I call the member function Y with out any mutex as the member function is on two separate instances. I am guessing the answer is no because member function Y is the same code in memory just wish different this pointers.

    Thanks any help

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,439
    Did you say "threads" because you think there is no other choice, or is this an exercise in specifically "using threads in a server"?

    Look specifically at the select() call; it allows you to observe many connected ports, and quickly dispatch the received data to an appropriate client handling instance.

    The first thing to do is separate out what will be "thread-specific" data and what will be "shared" data. If there is no shared data at all, it's pretty easy.

    Almost all of your shared data will need some kind of mutex around it. C++ (I assume, since you mention classes) would probably allow you to do something pretty clever without having to do too much when you use the data. But beware of out-smarting yourself on the implementation. Buggy thread code and obscure classes won't be a happy place to be.

    > Can I call the member function Y with out any mutex as the member function is on two separate instances.
    So long as the only thing accessed is in the class instance, it should be fine without locks.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions on multiple thread programming
    By lehe in forum C Programming
    Replies: 11
    Last Post: 03-27-2009, 07:44 AM
  2. Multiple thread and parallel programming
    By lehe in forum C Programming
    Replies: 1
    Last Post: 03-25-2009, 07:56 AM
  3. shutdown function socket programming
    By 256Doofus in forum Networking/Device Communication
    Replies: 0
    Last Post: 10-26-2008, 04:47 AM
  4. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  5. Linux Network Programming (server architeture)
    By curlious in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-08-2005, 04:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21