Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11
Hey everyone, I'm going to be writing a chat server in the near future, and need to find some good, detailed literature on inter-thread communication with posix threads. Anyone know ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2003
    Posts
    27

    pthread help


    Hey everyone,

    I'm going to be writing a chat server in the near future, and need to find some good, detailed literature on inter-thread communication with posix threads. Anyone know any good books/websites that I can check out?

  2. #2
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    Inter-thread _communication_? Well, you know, the thing with threads is that they don't usually need to communicate very much since they all run in the same VM. If you're looking for thread synchronizing, check out mutexes, condition variables and semaphores in the pthread section of the libc texinfo.

    Are you perfectly sure that you really want pthreads on just a chat server, though? If you don't know how to do thread-safe programming properly, they cause more problems than they solve.

  3. #3
    Linux Enthusiast
    Join Date
    Jun 2002
    Location
    San Antonio
    Posts
    621
    the two interprocess communication tools used for message passing are usually pipes (named or unnamed) and queues. Pipes are handled on the filesystem level, and message queues are handled on the OS level (I think).
    I respectfully decline the invitation to join your delusion.

  4. #4
    Just Joined!
    Join Date
    Apr 2003
    Posts
    27
    Dolda,

    If I don't use threads, my other options are:
    multi-process, which seems a bit too wasteful on memory, although I can readily use nice things such as pipes and queues like wassy pointed out,

    using some kind of function that blocks while waiting for a set of file descriptors to become available, like select(),

    or some kind of iterative approach, which would not be a very good idea for a chat server.

    It would seem that threads, although dangerous like you say because of the problem of functions being either reentrant or not reentrant, would be better because of the efficiency gain in memory usage, among other things.

    But, I could be entirely wrong, if you, or anyone else reading this, feels I am please tell me =]

    also, just to be clear, I am implementing this in C

  5. #5
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    There's most certainly nothing wrong with using select(), it's also by far more memory conservative than using threads, since you don't need a lot of extra kernel task structures and who knows what.
    Also, what you do yourself is far more dangerous than non-reentrant functions. Most functions are a bit reentrant, after all, or at least easy to control.
    Also, there's really nothing wrong with using a multi-process approach. Just fork a few times and have fun! It's far more failsafe than using multithreading, too, since one process can crash without affecting the others. It's not that resource-wasting, either, since forked processes use copy-on-write semantics for pages.

    What kind of advanced chat server is this, that requires multithreading, btw.? For all kinds of chat servers that I can think of, select() seems far more than enough.
    Always remember, that select() and poll() are your friends.

  6. #6
    Linux Enthusiast
    Join Date
    Jun 2002
    Location
    San Antonio
    Posts
    621
    poll() is not your friend unless you expect that resource to become available _very_ soon. It is better to use signals a good amount of the time, especially if you know how to implement them correctly.
    I respectfully decline the invitation to join your delusion.

  7. #7
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    Huh? What do you have against poll(), wassy? As I see it, it's like select, but a bit better, since you can explicitly wait for OOB data as well, and it specifies error events a bit better.

  8. #8
    Linux Enthusiast
    Join Date
    Jun 2002
    Location
    San Antonio
    Posts
    621
    basically poll() sits in a busy wait loop for data to become available. This is, in general, a Bad Thing. Signalling uses interrupts to let a process know when data has become available, a classic example of the publisher/subscriber pattern. It is like polling a button in java to see if it has been pressed yet. If you ask every 10th of a second it gets very CPU intensive. That is why you have actionListeners and etc.

    On the other hand poll() is great for some things. If you _know_ something is giong to happen in the next X milliseconds, then polling is the answer. To sit and wait forever is, in general, wrong though.
    I respectfully decline the invitation to join your delusion.

  9. #9
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    Uhmm... wassy? poll() is blocking.

  10. #10
    Just Joined!
    Join Date
    Apr 2003
    Posts
    27
    Today I took a quiz and I thought the problem, and its solution, might have relevence to this project. The problem was basically that you have to write a server application that uses two threads to handles connections and requests. A reader thread(1) will bind, listen, and accept on a socket, assume immediate transmission of data from the client, and receive a request from the client. You have to create a queue to keep track of what requests were made, and on what socket. The server thread(2) will take from the queue, and process the request.

    Since there is only two threads, I was thinking that I could take a similar approach where a client sends a message, it gets put on the queue by one thread, then the other thread takes the message off the queue, and sends them to everyone connected.

    Of course, mutexes will be used to appropriately lock and unlock the queue.

    What do you guys think? Select() and poll() still the better way to go?

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •