variable scope in threads

S

stephen

Hi

Just learning c and wonder if anyone can answer a question. I am
writing a program that uses threads. These threads will call a
specific function simultaneously. This specific function uses a global
pointer to run through a linked list of structures.

Thread1 enters the function and resets a global worker pointer to point
to the anchor of the linked list. It then continues to run through the
linked list. Meanwhile thread2 enters the function and resets the
global pointer to point to the anchor of the linked list. Am I to
understand that this will then cause problems with thread1 because it
is again pointing to the top of the linked list?

Now I have run into this problem I have gone away learnt threads (on
Windows, Red Hat and Solaris) and resolved the problem using both state
variables and mutexes (critical sections on Windows). Is there a
better and more simple way of doing this such as moving the global
pointer (the worker pointer that is not the anchor) into the function
itself? When threads enter the function are they still using the same
pointer or will each thread use a different copy>

I suppose the real question is when two threads call the same function
at the same time will they have there own copy of anylocal variables
within that function or do they still use the same copy of the local
variables?

Many thanks for everyones time!

Steve
 
I

Ian Collins

Hi

Just learning c and wonder if anyone can answer a question. I am
writing a program that uses threads. These threads will call a
specific function simultaneously. This specific function uses a global
pointer to run through a linked list of structures.

Thread1 enters the function and resets a global worker pointer to point
to the anchor of the linked list. It then continues to run through the
linked list. Meanwhile thread2 enters the function and resets the
global pointer to point to the anchor of the linked list. Am I to
understand that this will then cause problems with thread1 because it
is again pointing to the top of the linked list?
I'd suggest you post this to comp.programming.threads, where you will be
more on topic and get a more complete answer. Good old standard C has
no concept of threads.
I suppose the real question is when two threads call the same function
at the same time will they have there own copy of anylocal variables
within that function or do they still use the same copy of the local
variables?
If by local, you mean allocated off the stack then yes.
 
S

stephen

Sorry I probably should be more defined with my wording, By local I
mean the variable is locally defined within the source code.

The function in question is held within a shared library (dll on
Windows). The global structure pointed to by the anchor pointer is
built on the fly when the library is loaded, so I guess this *is* on
the stack?

Declared within the function in question in the source code is a
pointer which is set to the global anchor pointer to point to the start
of the global structure.

If two threads enter the function at the same time will they both being
using the same pointer? So if one thread sets the pointer to point to
one place and then a another thread sets it to point to another place
will the second thread cause problems for the first thread? ... i.e
causing it to loop maybe because it would keep getting reset to point
to the top of the list?

Steve
 
M

Mark McIntyre

Sorry I probably should be more defined with my wording, By local I
mean the variable is locally defined within the source code.

Doesn't matter how definite you are. This is still offtopic here, you
need to ask in a group that knows about threads.
so I guess this *is* on the stack?

C doesn't even require a stack to exist.
Mark McIntyre
 
I

Ian Collins

Sorry I probably should be more defined with my wording, By local I
mean the variable is locally defined within the source code.
Please
1 - provide context, quote what you are replying to.
2 - repost where I suggested, you will find all of your answers there.
 
S

stephen

1. You can see the post thread.
2. You didn't actually suggest anywhere, hence my last question!

If you don't know I will ask someone else, many thanks for your time.
 
A

Artie Gold

1. You can see the post thread.

Wrong -- at least not if you use a real newsreader (in general).
2. You didn't actually suggest anywhere, hence my last question!
It was suggested. Since ISO C has no support for threads,
would be a good choice.
If you don't know I will ask someone else, many thanks for your time.
You've asked the world already.

--ag

[Who wishes that people would read FAQs or at the very least lurk before
posting...and not assume that Google's broken usenet interface is the
world.]
 
K

Keith Thompson

Do you know of any groups discussing this topic?

Discussing what topic?

Ian Collins already suggested a newsgroup.

Please provide context when you post a followup. Most of us don't use
Google, and we can't necessarily see the article to which you're
replying. Read <http://cfaj.freeshell.org/google/>. I'm sure the
advice will serve you well in <HINT>comp.programming.threads</HINT> as
well as here.
 
K

Keith Thompson

1. You can see the post thread.

Perhaps you can. Don't assume everyone else can as well.

Google is giving you a very distorted view of Usenet. Read
2. You didn't actually suggest anywhere, hence my last question!

He most certainly did. Just a few articles up in this very thread,
Ian Collins wrote:

] I'd suggest you post this to comp.programming.threads, where you will
] be more on topic and get a more complete answer. Good old standard C
] has no concept of threads.

Please pay attention.
 
F

Flash Gordon

1. You can see the post thread.

Google is *not* Usenet. The way Usenet works people might not have
received the post you are replying to. That is reality, and it really
does happen.
2. You didn't actually suggest anywhere, hence my last question!

If you don't know I will ask someone else, many thanks for your time.

If you want to talk about threads, try a group with threads in the name.
--
Flash Gordon
Living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidlines and intro -
http://clc-wiki.net/wiki/Intro_to_clc
 
S

stephen

# Wrong -- at least not if you use a real newsreader (in general).

Just using google and a Web browser! Google shows me the threads, not
my problem if your news reader doesn't show you, perhaps you should
complain to the athor!

# It was suggested. Since ISO C has no support for threads,
# would be a good choice.

I have no way implied that ISO c facilitates threads.

# You've asked the world already.

While I can't garantuee it, I am pretty sure that every programmer in
the world does not read this news group!
 
S

stephen

Not my problem if you can't see the post thread! You didn't mention
any links in your reply (someone else did). If you know the answer
then why not just answer me or knidly tell me to use a different news
group.

Most people in this news group appear to display the same incompetence.
People post here to learn, while few do help others seem to want to
point out where you are going wrong when they don't "realy" know the
answer!

If you realy did know the answer you would tell me :) No sweat, I
have just unsubscribed to this group so don't even bother replying!
 
C

CBFalconer

1. You can see the post thread.
2. You didn't actually suggest anywhere, hence my last question!

If you don't know I will ask someone else, many thanks for your time.

You leave the impression of being unusually thick, or incapable of
reading a complete answer. GOOGLE IS NOT USENET. Your view is
distorted. No, 'you' cannot see the "post thread", whatever that
may be. 'You' did suggest a newsgroup. You consistently failed to
follow the advice given on providing context, and the means to do
so on the broken google interface.

So far you are a prime example of why many have simply PLONKED all
google posters, meaning that they will never see nor read any such
postings.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
M

Mark McIntyre

# Wrong -- at least not if you use a real newsreader (in general).

Just using google and a Web browser! Google shows me the threads, not
my problem if your news reader doesn't show you, perhaps you should
complain to the athor!

Or perhaps you should get an understanding of how usenet works. For
instance
- the previous posts may have been deleted from whatever server one is
accessing, for space reasons or policy reasons
- the previous posts may not yet have propagated round the world
- the previous posts may have been deleted from one's client
and so forth.
# You've asked the world already.

While I can't garantuee it, I am pretty sure that every programmer in
the world does not read this news group!

Do you have a better way to reach an audience of thousands? Then go to
it.
Mark McIntyre
 
M

Mark McIntyre

Not my problem if you can't see the post thread!

I'm afraid it is. If you don't show context in your messages to CLC,
you will rapidly get killfiled by the regulars, and obtain no help,
only complaints.
Most people in this news group appear to display the same incompetence.

As you?
If you realy did know the answer you would tell me :)

Bullshit. Say you're at Anfield watching your favorite team in the
cup, and someone comes up and asks you for the general solution to a
quartic equation, are you going to stop watching the game so you can
deal with your annoying friend? Or tell him to bugger off and ask a
mathematician in the university.
No sweat, I
have just unsubscribed to this group so don't even bother replying!

you can't unsubscribe to usenet.
Mark McIntyre
 
K

Keith Thompson

Mark McIntyre said:
On 17 Feb 2006 15:15:41 -0800, in comp.lang.c ,


you can't unsubscribe to usenet.

Every newsreader I've ever used has a command to "unsubscribe" to a
specified newsgroup. Presumably Google provides something similar.
And he does raise one valid point; why did you bother replying?
 
M

Mark McIntyre

Every newsreader I've ever used has a command to "unsubscribe" to a
specified newsgroup. Presumably Google provides something similar.

You still can't unsubscribe from usenet. I'm sorry if you missed the
sarcasm of my remark.
And he does raise one valid point; why did you bother replying?

IME people who say "I'm not listening, da da da hum hum, can't hear
you" are in fact listening but hoping to "get the last word"...

Mark McIntyre
 

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
474,175
Messages
2,570,944
Members
47,491
Latest member
mohitk

Latest Threads

Top