Re: Sharing a struct between threads?
- From: Brian C <brianc@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sat, 28 Oct 2006 03:43:39 GMT
Bill Pursell wrote:
Brian C wrote: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).Derek Ho wrote:<snip>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:
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
.
- References:
- Sharing a struct between threads?
- From: Derek Ho
- Re: Sharing a struct between threads?
- From: Brian C
- Re: Sharing a struct between threads?
- From: Bill Pursell
- Sharing a struct between threads?
- Prev by Date: Re: replacement for timeGetTime
- Next by Date: Re: Starting an external process from C++
- Previous by thread: Re: Sharing a struct between threads?
- Next by thread: Re: Sharing a struct between threads?
- Index(es):
Relevant Pages
|
|