Multithreading question

F

facugaich

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.
 
K

Keith Thompson

facugaich said:
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.)
 
F

facugaich

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

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...
 
S

santosh

facugaich said:
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.
 
I

Ian Collins

facugaich said:
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.
 
A

av

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)
 
A

Ancient_Hacker

facugaich said:
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.
 
J

Jordan Abel

2006-10-10 said:
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?
 
A

Ancient_Hacker

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.
 
F

facugaich

Jordan said:
2006-10-10 said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top