473,396 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Multithreading question

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.

Oct 9 '06 #1
9 2278
"facugaich" <fa*******@gmail.comwrites:
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) ks***@mib.org <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.
Oct 10 '06 #2

Keith Thompson wrote:
"facugaich" <fa*******@gmail.comwrites:
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) ks***@mib.org <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...

Oct 10 '06 #3
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.

Oct 10 '06 #4
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.
Oct 10 '06 #5
av
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)
Oct 10 '06 #6

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.

Oct 10 '06 #7
2006-10-10 <11**********************@b28g2000cwb.googlegroups .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?
Oct 10 '06 #8

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.

Oct 10 '06 #9

Jordan Abel wrote:
2006-10-10 <11**********************@b28g2000cwb.googlegroups .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.

Oct 10 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jean-Yves Nief | last post by:
hello, I have written a script which is performing some tasks in multithreading mode: the main thread is opening a connection to a distant server and all the threads that I start will have to...
1
by: dixp | last post by:
I'm new to writing multithreaded apps and I have a design question. I have a winforms app and a class which has a method that does processing which is time intensive. I want the user to be able...
0
by: GianGuz | last post by:
In the following example Global is able to create and manage access to objects of any kind (even in a multithreading environment) with and index value attached to. So a Global<0, string> is a...
11
by: Mark Yudkin | last post by:
The documentation is unclear (at least to me) on the permissibility of accessing DB2 (8.1.5) concurrently on and from Windows 2000 / XP / 2003, with separate transactions scope, from separate...
16
by: Robert Zurer | last post by:
Can anyone suggest the best book or part of a book on this subject. I'm looking for an in-depth treatment with examples in C# TIA Robert Zurer robert@zurer.com
10
by: Marek | last post by:
Hi, I am analyzing Duwamish7 source code boundled with Visual Studio .NET 2003. Could anoybody explain why the Monitor.Enter and Monitor.Exit block is used inside a static constructor? The code...
2
by: Rich | last post by:
Hello, I have set up a multithreading routine in a Test VB.net proj, and it appears to be working OK in debug mode and I am not using synchronization. Multithreading is a new thing for me, and...
2
by: Multithreading problem in vb.net | last post by:
Greetings, I am new to multithreading and I am trying to implement it in my app. This application is distributed application which needs to refresh every say 5 secs to show some activities in...
6
by: MeowCow | last post by:
I will try and make my question with out being too long winded. I have been doing a lot of reading on how to do multithreading and I have implemented the code from the following example on...
7
by: Ray | last post by:
Hello, Greetings! I'm looking for a solid C++ multithreading book. Can you recommend one? I don't think I've seen a multithreading C++ book that everybody thinks is good (like Effective C++ or...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.