Multithreading question

Discussion in 'C Programming' started by facugaich, Oct 10, 2006.

  1. facugaich

    facugaich Guest

    Hi, I have some questions regarding a multithreaded server I'm working
    on.

    Since the main function (the primary thread) and the handle_client
    function (called by all the other threads) use the user database
    variables (pointer to array of struct, file, number of users) and to
    the handle_function I can only pass a void* (currently a pointer to a
    struct with client data), I have declared the user database variables
    as global. Do you think they're justified in this case?

    Also, I'm an intermediate (I think) C programmer and so far in
    everything I have read, I find situations where the code is correct but
    it could be rewritten with lots of changes to avoid bugs, better
    portability, etc. These details are never mentioned in tutorials (sadly
    I can't afford a book) so maybe I was thinking I could post the code of
    my current work and get some advice. It has 480 lines, documented,
    would it be ok to post it in here? If not, do you know any place I
    could do so? If also no, what advice would you give me for the issue?

    Thanks in advance.
    facugaich, Oct 10, 2006
    #1
    1. Advertising

  2. "facugaich" <> writes:
    > Hi, I have some questions regarding a multithreaded server I'm working
    > on.


    Try comp.programming.threads. (Thread support isn't part of standard C.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Oct 10, 2006
    #2
    1. Advertising

  3. facugaich

    facugaich Guest

    Keith Thompson wrote:
    > "facugaich" <> writes:
    > > Hi, I have some questions regarding a multithreaded server I'm working
    > > on.

    >
    > Try comp.programming.threads. (Thread support isn't part of standard C.)
    >
    > --
    > Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    > San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    > We must do something. This is something. Therefore, we must do this.


    Actually, if you read the question (in the following paragraph), it has
    nothing to do with threads (It's about globals), I'm very sorry for
    misnaming the topic...
    facugaich, Oct 10, 2006
    #3
  4. facugaich

    santosh Guest

    facugaich wrote:
    > Hi, I have some questions regarding a multithreaded server I'm working
    > on.
    >
    > Since the main function (the primary thread) and the handle_client
    > function (called by all the other threads) use the user database
    > variables (pointer to array of struct, file, number of users) and to
    > the handle_function I can only pass a void* (currently a pointer to a
    > struct with client data), I have declared the user database variables
    > as global. Do you think they're justified in this case?


    What case? Without clearer explanation and without actually seeing the
    code, any specific advice would be a Shot in the Dark. In general,
    global data can be a source of subtle bugs in multi-threaded programs.
    You should strive to use local variables as much as possible and use
    globals only when they're really required.

    > Also, I'm an intermediate (I think) C programmer and so far in
    > everything I have read, I find situations where the code is correct but
    > it could be rewritten with lots of changes to avoid bugs, better
    > portability, etc. These details are never mentioned in tutorials (sadly
    > I can't afford a book) so maybe I was thinking I could post the code of
    > my current work and get some advice. It has 480 lines, documented,
    > would it be ok to post it in here? If not, do you know any place I
    > could do so? If also no, what advice would you give me for the issue?


    Only very few programmers can write picture-perfect code in the first
    iteration. And yes, lots of open source programs are not good examples
    of the best of programming practices.

    If you want the group to review anything beyond a small program, maybe
    posting a link to the sources would be a better idea. But it may be
    better to ask for advice in a picemeal fashion rather than for the
    whole program. Many may not be eager to go through hundreds of lines of
    source. Also be warned that if the source makes extensive use of
    non-standard C, then feedback from this group may not be what you
    expect and is likely to pertain only to the standard based parts of
    your code.
    santosh, Oct 10, 2006
    #4
  5. facugaich

    Ian Collins Guest

    facugaich wrote:
    > Hi, I have some questions regarding a multithreaded server I'm working
    > on.
    >
    > Since the main function (the primary thread) and the handle_client
    > function (called by all the other threads) use the user database
    > variables (pointer to array of struct, file, number of users) and to
    > the handle_function I can only pass a void* (currently a pointer to a
    > struct with client data), I have declared the user database variables
    > as global. Do you think they're justified in this case?
    >

    A posting to comp.programming.threads would be wise if you want to
    discuss issues involving globals in a threaded application.

    --
    Ian Collins.
    Ian Collins, Oct 10, 2006
    #5
  6. facugaich

    av Guest

    On 9 Oct 2006 20:11:32 -0700, santosh wrote:
    >facugaich wrote:
    >> Hi, I have some questions regarding a multithreaded server I'm working
    >> on.
    >>
    >> Since the main function (the primary thread) and the handle_client
    >> function (called by all the other threads) use the user database
    >> variables (pointer to array of struct, file, number of users) and to
    >> the handle_function I can only pass a void* (currently a pointer to a
    >> struct with client data), I have declared the user database variables
    >> as global. Do you think they're justified in this case?

    >
    >What case? Without clearer explanation and without actually seeing the
    >code, any specific advice would be a Shot in the Dark. In general,
    >global data can be a source of subtle bugs in multi-threaded programs.


    so in a multithread program there is 1 alone stack for program or
    there are one stack each thread?
    i suppose the 2 is rigth (otherwise to call, or to put parameters in
    the stack should be not safe)

    if this is true someone can see that each tread can have a pointer to
    a different data section (of the same dimension) and the offset from
    that pointer it is the same in all them (data section in each tread)
    av, Oct 10, 2006
    #6
  7. facugaich wrote:
    > I have declared the user database variables
    > as global. Do you think they're justified in this case?


    It depends. If you're just reading those variables from the threads,
    that should be okay.

    But if anything is changing them, you need some sort of software
    interlock to prevent simultaneous reading and writing.

    You could post a link to your code, but this newsgroup is more oriented
    towards really picky issues about the C standards, so you're likely to
    get 68 replies about terribly minor issues, which are okay to point out
    I guess, but they tend to hide any major issues.
    Ancient_Hacker, Oct 10, 2006
    #7
  8. facugaich

    Jordan Abel Guest

    2006-10-10 <>,
    Ancient_Hacker wrote:
    > It depends. If you're just reading those variables from the threads,
    > that should be okay.
    >
    > But if anything is changing them, you need some sort of software
    > interlock to prevent simultaneous reading and writing.
    >
    > You could post a link to your code, but this newsgroup is more
    > oriented towards really picky issues about the C standards, so you're
    > likely to get 68 replies about terribly minor issues, which are okay
    > to point out I guess, but they tend to hide any major issues.


    You mean major issues like the fact that his real problem has absolutely
    nothing to do with multithreading as such, but rather he's nervous about
    using void * for callbacks so he's making an [ill-advised] attempt to
    sidestep the problem by using globals?
    Jordan Abel, Oct 10, 2006
    #8
  9. Jordan Abel wrote:


    > You mean major issues like the fact that his real problem has absolutely
    > nothing to do with multithreading as such, but rather he's nervous about
    > using void * for callbacks so he's making an [ill-advised] attempt to
    > sidestep the problem by using globals?


    Huh? He seems to be up to speed on using the void* parameter to pass
    in thread-specific data.

    And using globals to pass information isnt necessarily a major issue as
    long as they're interlocked properly. And interlocking isnt just an
    issue with globals, any shared data passed inthru the void * struct is
    also going to require interlocks.
    Ancient_Hacker, Oct 10, 2006
    #9
  10. facugaich

    facugaich Guest

    Jordan Abel wrote:
    > 2006-10-10 <>,
    > Ancient_Hacker wrote:
    > > It depends. If you're just reading those variables from the threads,
    > > that should be okay.
    > >
    > > But if anything is changing them, you need some sort of software
    > > interlock to prevent simultaneous reading and writing.
    > >
    > > You could post a link to your code, but this newsgroup is more
    > > oriented towards really picky issues about the C standards, so you're
    > > likely to get 68 replies about terribly minor issues, which are okay
    > > to point out I guess, but they tend to hide any major issues.

    >
    > You mean major issues like the fact that his real problem has absolutely
    > nothing to do with multithreading as such, but rather he's nervous about
    > using void * for callbacks so he's making an [ill-advised] attempt to
    > sidestep the problem by using globals?


    As someone said, I'm using void* to pass a pointer to a struct with
    thread-specific variables, this struct is previoulsy dinamically
    allocated when each new thread is created (before, actually). So,
    filling this struct with common data for every thread seems like a
    (great) waste of memory, hence me using globals.

    Thanks to everyone who replied, but this topic has gone a little OT so
    I think we should let it die now. I was able to clarify some of the
    doubts I had.
    facugaich, Oct 10, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob

    multithreading question

    Rob, Sep 8, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    364
  2. Jean-Yves Nief

    question on multithreading, pipes

    Jean-Yves Nief, Dec 15, 2003, in forum: Python
    Replies:
    1
    Views:
    338
  3. Mark English

    Multithreading tkinter question

    Mark English, Dec 15, 2004, in forum: Python
    Replies:
    3
    Views:
    614
    Eric Brunel
    Dec 17, 2004
  4. Alexander Dong Back Kim

    A simple question on multithreading

    Alexander Dong Back Kim, Mar 19, 2008, in forum: C++
    Replies:
    2
    Views:
    258
    Chris Thomasson
    Mar 19, 2008
  5. PJ6

    multithreading question

    PJ6, Nov 29, 2009, in forum: ASP .Net
    Replies:
    1
    Views:
    302
Loading...

Share This Page