Thread: fork or thread in a server/client project

  1. #16
    Registered User
    Join Date
    Dec 2006
    Posts
    19
    Just wondering...
    I'm going to start a new thread each time there is a new connection on the port I'm listening no. The program runs in an eternal loop. So I can't just put the threads in a for loop and then know how many threads that have been running. But when the program ends I have to wait for all the running threads to finish, but how do I do that when I don't know how many threads that have been running.

    Of course I can have a counter that counts up the threads, but eventually this counter has to have a limit. And what do I do then, when the counter reaches the limit?

  2. #17
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by daghenningsorbo View Post
    Of course I can have a counter that counts up the threads, but eventually this counter has to have a limit. And what do I do then, when the counter reaches the limit?
    I presume the threads are not persistent, ie, they add a message to the queue then die.

    Presuming that, I wouldn't worry about a limit, the server must answer one call at a time anyway (in fact, thinking harder, I doubt you need to thread or fork these calls at all. The server will not drop incoming messages and this will not cause any noticeable delay, honestly).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #18
    Registered User
    Join Date
    Dec 2006
    Posts
    19
    Thanks for the reply. I might have done the task without threads, but the assignment specifies that the application should be able to work asynchronously, hence I have to use threads.

    I have now implemented threads and it's working flawlessly!

    Another question tho...
    "Do I have to use a mutex lock when only reading the queue? For instance if one thread writes to the queue (with mutex lock) exactly when another one reads from it (without mutex lock), then I might get unpredicted results, right? Or am I wrong?

  4. #19
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> Do I have to use a mutex lock when only reading the queue?
    Most likely. Assuming "reading the Q" involves unllinking a node from a list etc...

    gg

  5. #20
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by daghenningsorbo View Post
    Another question tho...
    "Do I have to use a mutex lock when only reading the queue? For instance if one thread writes to the queue (with mutex lock) exactly when another one reads from it (without mutex lock), then I might get unpredicted results, right? Or am I wrong?
    I guess this depends on the nature of the write. If all the threads do is add a link to the end of the queue, then the location and content of all the other links is not being changed, so everything will be okay. If the threads potentially insert something, moving some of the links around, then you must have a lock to prevent errors.

    Which you might want to have one just to be safe anyway, particularly if this is being marked for "best practices" kind of stuff.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #21
    Registered User
    Join Date
    Dec 2006
    Posts
    19
    Another question

    Is it possible to call other functions within the thread-function? Will the variables in the other functions be own instances for every thread?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Terminating secondary thread from another thread
    By wssoh85 in forum C++ Programming
    Replies: 13
    Last Post: 12-19-2008, 05:14 AM
  2. Messaging Between Threads, Exiting Thread On Demand, Etc.
    By HyperShadow in forum C++ Programming
    Replies: 10
    Last Post: 06-09-2007, 01:04 PM
  3. fork() inside a thread
    By rpalmer in forum C Programming
    Replies: 3
    Last Post: 05-25-2007, 02:29 AM
  4. [code] Win32 Thread Object
    By Codeplug in forum Windows Programming
    Replies: 0
    Last Post: 06-03-2005, 03:55 PM
  5. How to add a file to a project in bloodshed.
    By Smeep in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2005, 09:29 PM

Tags for this Thread