#Passing a struct in pthreads

11 messages · Page 1 of 1 (latest)

rich halo
#

Hello, I am working on an assignment that uses multithreading using pthreads in c++. I've created a struct which stores the game data. The fields are:
A void pointer (which I'm using to store a pointer to a multiarray of unsigned chars, created before being passed)
Two integers storing the amount of squares each team possesses
One boolean which stores a true/false value that the supervisor thread will set to make all executing threads exit their loops.

The problem is, I'm getting inaccurate values when attempting to access values from the struct that the pthreads are accessing. I'm using mutexes to lock the threads before accessing the memory at any point, and then unlocking afterwards.

opaque cryptBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

rich halo
#

I'm wondering how a pthread accesses a pointer to a struct

#

the struct is passed as a void pointer

#

and if I want to access the value of the boolean in the struct I am using the key:

#

*((gameData*) &game)).runGame

#

game is the passed pointer to the struct

#

gameData is the struct type

#

and runGame is the boolean

#

nvm got it working by just skipping the entire void pointer pass