Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Multithreading question

Reply
Thread Tools

Multithreading question

 
 
facugaich
Guest
Posts: n/a
 
      10-10-2006
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.

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      10-10-2006
"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) kst- <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.
 
Reply With Quote
 
 
 
 
facugaich
Guest
Posts: n/a
 
      10-10-2006

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) kst- <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...

 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      10-10-2006
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.

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      10-10-2006
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.
 
Reply With Quote
 
av
Guest
Posts: n/a
 
      10-10-2006
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)
 
Reply With Quote
 
Ancient_Hacker
Guest
Posts: n/a
 
      10-10-2006

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.

 
Reply With Quote
 
Jordan Abel
Guest
Posts: n/a
 
      10-10-2006
2006-10-10 < .com>,
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?
 
Reply With Quote
 
Ancient_Hacker
Guest
Posts: n/a
 
      10-10-2006

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.

 
Reply With Quote
 
facugaich
Guest
Posts: n/a
 
      10-10-2006

Jordan Abel wrote:
> 2006-10-10 < .com>,
> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
multithreading question PJ6 ASP .Net 1 11-29-2009 12:30 AM
A simple question on multithreading Alexander Dong Back Kim C++ 2 03-19-2008 06:31 AM
Multithreading tkinter question Mark English Python 3 12-17-2004 08:48 AM
multithreading question Rob ASP .Net 4 09-09-2004 04:30 PM
question on multithreading, pipes Jean-Yves Nief Python 1 12-24-2003 03:52 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57