Unix Programming - Sharing a struct between threads?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > October 2006 > Sharing a struct between threads?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Sharing a struct between threads?
Derek Ho

2006-10-26, 7:19 pm

I am currently creating a "train scheduler" for class, using multiple
threads. Please forgive my ignorance on this topic, but I created a
struct in my main( ) but cant seem to access the struct in the thread
functions. I am given these error outputs:

assign3.c: In function `ArriveBridge':
assign3.c:84: error: `westYard' undeclared (first use in this function)
assign3.c:84: error: (Each undeclared identifier is reported only once
assign3.c:84: error: for each function it appears in.)
assign3.c:91: error: `eastYard' undeclared (first use in this function)

I have thought that threads share variables inside the main( ), so I
have thought that global structs created in the main could also be
shared and accessed by multiple threads.

Thank you in advance.

Derek H.

Logan Shaw

2006-10-27, 1:24 am

Derek Ho wrote:
> I am currently creating a "train scheduler" for class, using multiple
> threads. Please forgive my ignorance on this topic, but I created a
> struct in my main( ) but cant seem to access the struct in the thread
> functions. I am given these error outputs:
>
> assign3.c: In function `ArriveBridge':
> assign3.c:84: error: `westYard' undeclared (first use in this function)
> assign3.c:84: error: (Each undeclared identifier is reported only once
> assign3.c:84: error: for each function it appears in.)
> assign3.c:91: error: `eastYard' undeclared (first use in this function)
>
> I have thought that threads share variables inside the main( ), so I
> have thought that global structs created in the main could also be


Your problem appears to be the idea that anything global exists inside
of main(). main() is a function, so all variables declared inside it
have the function (only) as their scope.

Your problem really has nothing to do with threads, but I will offer
a piece of advice: if you wish to share local variables between
threads, be careful about the lifetimes of variables. This function
is perfectly OK:

void foo (void)
{
int i, j;

i = 2;
j = 5;

do_bar_action (& i);
do_bar_action (& j);
}

However, this one is not:

void foo (void)
{
int i, j;

i = 2;
j = 5;

start_thread_to_do_bar_action (& i);
start_thread_to_do_bar_action (& j);
}

The reason is that in the first example, do_bar_action() returns before
i and j go out of scope. In the second, threads are started and passed
pointers to i and j. The threads continue to run and may reference i
and j even after the storage for i and j is reclaimed and used for other
purposes.

- Logan
Brian C

2006-10-27, 1:24 am

Derek Ho wrote:
> I am currently creating a "train scheduler" for class, using multiple
> threads. Please forgive my ignorance on this topic, but I created a
> struct in my main( ) but cant seem to access the struct in the thread
> functions. I am given these error outputs:
>
> assign3.c: In function `ArriveBridge':
> assign3.c:84: error: `westYard' undeclared (first use in this function)
> assign3.c:84: error: (Each undeclared identifier is reported only once
> assign3.c:84: error: for each function it appears in.)
> assign3.c:91: error: `eastYard' undeclared (first use in this function)
>
> I have thought that threads share variables inside the main( ), so I
> have thought that global structs created in the main could also be
> shared and accessed by multiple threads.
>
> Thank you in advance.
>
> Derek H.
>

Actually,
Anything inside of main() is scoped to main(). This means that only
main() can access the data.
If you move the struct to global memory, be sure to provide a lock of
some sorts when modifying it.
Another option you can use instead of global memory is to pass a
pointer to the thread with your struct. For example:

typedef struct
{
int ArrivalTime;
int DepartureTime;
int TicketsSold;
} BusSchedule;
int main(void)
{
BusSchedule MySchedule;

...
if(pthread_create(...,...,&BusScheduleStart,&MySchedule)
...
}


int *BusScheduleStart(void *Arg)
{
BusSchedule *MySchedule=(BusSchedule *)Arg;
}



Don't quote me on the syntax, but that is how you'd pass your struct to
your threads avoiding using global memory.
Brian C

2006-10-27, 1:24 am

Brian C wrote:
> Derek Ho wrote:
> Actually,
> Anything inside of main() is scoped to main(). This means that only
> main() can access the data.
> If you move the struct to global memory, be sure to provide a lock
> of some sorts when modifying it.
> Another option you can use instead of global memory is to pass a
> pointer to the thread with your struct. For example:
>
> typedef struct
> {
> int ArrivalTime;
> int DepartureTime;
> int TicketsSold;
> } BusSchedule;
> int main(void)
> {
> BusSchedule MySchedule;
>
> ...
> if(pthread_create(...,...,&BusScheduleStart,&MySchedule)
> ...
> }
>
>
> int *BusScheduleStart(void *Arg)
> {
> BusSchedule *MySchedule=(BusSchedule *)Arg;
> }
>
>
>
> Don't quote me on the syntax, but that is how you'd pass your struct to
> your threads avoiding using global memory.

Also, even with this option, you should put a lock into the struct. For
example:

typedef struct
{
int ArrivalTime;
int DepartureTime;
int TicketsSold;
pthread_mutex_lock RecordLock;
} BusSchedule;

Locks are weird because if you run on a single processor, you're
typically ok. When you move the same code to a multi-processor machine,
locks become an issue.
Bill Pursell

2006-10-27, 1:24 am

Brian C wrote:
> Derek Ho wrote:
<snip>[vbcol=seagreen]
>
> Anything inside of main() is scoped to main(). This means that only
> main() can access the data.
> If you move the struct to global memory, be sure to provide a lock of
> some sorts when modifying it.
> Another option you can use instead of global memory is to pass a
> pointer to the thread with your struct. For example:
>
> typedef struct
> {
> int ArrivalTime;
> int DepartureTime;
> int TicketsSold;
> } BusSchedule;
> int main(void)
> {
> BusSchedule MySchedule;
>
> ...
> if(pthread_create(...,...,&BusScheduleStart,&MySchedule)
> ...
> }


Derek,

Make sure that you wait for the thread before you allow
main to terminate. (ie, make sure that "..." includes
a call to pthread_join() (or some other synchronization
mechanism, although pthread_join() is simplest.)).

--
Bill Pursell

A. Melinte

2006-10-27, 7:15 pm

>
> assign3.c: In function `ArriveBridge':
> assign3.c:84: error: `westYard' undeclared (first use in this function)
> assign3.c:84: error: (Each undeclared identifier is reported only once
> assign3.c:84: error: for each function it appears in.)
> assign3.c:91: error: `eastYard' undeclared (first use in this function)
>
> I have thought that threads share variables inside the main( ), so I
> have thought that global structs created in the main could also be
> shared and accessed by multiple threads.


Yes, threads do share memory. What you have there are compiler errors -
compiler does not know what westYard is. That struct definition must be in
a header, visible where you want to use it.


A. Melinte

2006-10-27, 7:15 pm


"A. Melinte" <nospam@nospam.com> wrote in message
news:ehtj4p$4em$1@engnntp1.cig.mot.com...
>
> Yes, threads do share memory. What you have there are compiler errors -
> compiler does not know what westYard is. That struct definition must be

in
> a header, visible where you want to use it.
>


and the variables too should be declared extern at that point



Brian C

2006-10-28, 1:31 am

Bill Pursell wrote:
> Brian C wrote:
> <snip>
>
> Derek,
>
> Make sure that you wait for the thread before you allow
> main to terminate. (ie, make sure that "..." includes
> a call to pthread_join() (or some other synchronization
> mechanism, although pthread_join() is simplest.)).
>
> --
> Bill Pursell
>

Yes of course, I did not think of that. Usually my main()'s continue
working on other things, but, yes, if main() won't be doing any work,
you must wait on your threads to finish up otherwise main() will just
fall through and exit the program (threads included).
David Schwartz

2006-10-28, 1:40 pm


Derek Ho wrote:

> I am currently creating a "train scheduler" for class, using multiple
> threads. Please forgive my ignorance on this topic, but I created a
> struct in my main( ) but cant seem to access the struct in the thread
> functions. I am given these error outputs:
>
> assign3.c: In function `ArriveBridge':
> assign3.c:84: error: `westYard' undeclared (first use in this function)
> assign3.c:84: error: (Each undeclared identifier is reported only once
> assign3.c:84: error: for each function it appears in.)
> assign3.c:91: error: `eastYard' undeclared (first use in this function)


These errors mean exactly what they say. A C compiler must see the
definition of something before the first time you use it.

> I have thought that threads share variables inside the main( ), so I
> have thought that global structs created in the main could also be
> shared and accessed by multiple threads.


You are confusing two very different types of sharing. Threads share
variables in the sense that any thread can modify any variable if it
knows its address.

However, when the compiler is compiling the code for a function that
you are later going to run in a thread, it neither knows or cares about
that. It's just compiling the function, whether it is going to be in a
thread or not. For example:

void foo(void)
{
j++;
}

void bar(void)
{
int j=0;
/* some more code here */
}

Do you think a compiler is going to look at this and say "maybe 'foo'
and 'bar' will be running at the same time, so I'll hook that 'j++' in
'foo' up to the 'j' in bar?"

What if 'bar' returns before 'foo' and 'j' no longer exists? What if
two threads are each in 'bar' -- which one should 'foo' be hooked up
to?

What if the next bit of code is:

void baz(void)
{
int j=0;
/* some code here */
}

Now which 'j' should 'foo' be hooked up to?

Fortunately for all of us, threaded code follows the same variable
scoping and lifetime rules as non-threaded code.

DS

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com