Re: Sharing a struct between threads?



Bill Pursell wrote:
Brian C wrote:
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:
<snip>
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

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



Relevant Pages

  • Re: Sharing a struct between threads?
    ... Derek Ho wrote: ... struct in my mainbut cant seem to access the struct in the thread ... Another option you can use instead of global memory is to pass a pointer to the thread with your struct. ...
    (comp.unix.programmer)
  • Re: Sharing a struct between threads?
    ... Please forgive my ignorance on this topic, ... struct in my mainbut cant seem to access the struct in the thread ... If you move the struct to global memory, be sure to provide a lock of ...
    (comp.unix.programmer)
  • Re: debugging
    ... Bill Pursell skrev: ... void pretty_print (struct foo *f) ... void *ret; ...
    (comp.lang.c)
  • Re: passing arguments from bootloader to kernel
    ... There is no official way to pass parameters between bootloader and ... How you do it (meaning what global memory you choose to use) is up to ... >> struct BOOT_ARGS at address 0xA00FFC00 (used by the bootloader and ... but this is not where and what the kernel ...
    (microsoft.public.windowsce.platbuilder)
  • Re: dealing with binary files
    ... Tom Brown wrote: ... at this point i cant do more than this, because i cant figure out what ... You are looking at the correct module, struct. ... understand or even correctly extract data from a binary file, ...
    (comp.lang.python)