Xaphier Report post Posted October 26, 2005 Hi all! I am working at a new view frustum system and my problem is that I need read-write-locks. (I need them for a lot of situations because network and particle threads can acces and/or change the data that is needed for rendering. Using a mutext would be verry bad, because the particle thread would lock the system and the rendere would not be able to us it. On the other side the particle thread deletes particles and the renderer just need read acces to them.) Any ideas what I can do? SDL do not have read-write-lock. Share this post Link to post Share on other sites
Zep Report post Posted October 26, 2005 (edited) Guess u could use mutexes SDL_mutex *frustum_mutex = NULL; frustum_mutex = SDL_CreateMutex(); then just, SDL_LockMutex(frustum_mutex); code here that modifes variable. SDL_UnlockMutex(frustum_mutex); And then, mutex both reads and writes on seperate mutex's, So they wont overlap, add a poll function on the read mutex's, to make sure the writes are unlocked before using the var? Edited October 26, 2005 by Zep Share this post Link to post Share on other sites
Wytter Report post Posted October 27, 2005 To create an overview of the problem: Renderer thread needs read/write access to the particles - it will add particles when asked by the server Timer thread needs read/write access to the particles - it will change and delete particles We could solve the problem by moving the deletion of particles to the renderer thread... Share this post Link to post Share on other sites
Wytter Report post Posted October 27, 2005 OK, Xaphier and I have discussed the problem more indepth and decided to go with adding a new queue for object changes. This way we solve the problem with the timer thread deleting particles and we avoid a lot of problems with add/removes in the new frustum system. Plus it's a much simpler approach so it should be a lot easier to get it working. From queue.h: typedef struct node { void *data; struct node *next; /* Pointer to the next node */ } node_t; The data will in this case be: typedef struct { Uint8 type; //OBJ_2D_ADD, OBJ_2D_REMOVE, OBJ_3D_ADD, OBJ_3D_REMOVE, OBJ_LIGHT_ADD, OBJ_LIGHT_REMOVE, OBJ_PART_ADD, OBJ_PART_REMOVE Uint16 offset;//The offset in obj_2d_list[], objects_list[], lights_list[], particle_list[] } object_change; We'll have a global array: #define MAX_OBJECT_CHANGES 3000 object_change object_changes[MAX_OBJECT_CHANGES]; int object_change_offset; And then when doing the queue_push it will add the data of &object_changes[object_change_offset]; and next increase object_change_offset. We also need a function that can read the current queue without popping it. Share this post Link to post Share on other sites
Vegar Report post Posted October 27, 2005 We also need a function that can read the current queue without popping it. You could use queue_front_node() to get the first node and then use ->next to descend. Share this post Link to post Share on other sites