Edit: The reason queue is 2d is because I need a pointer of Command so that cmd can equal NULL. NULL == (void *). This is where I get confused though, and why I've come here. :)
To help try and figure out another problem I have in Python, I'm implementing a small test program in C. While I know a little, apparently I'm confused. I'm trying to write a simple queue to be used in asynchronous USB transfers. Something's not right with queue, as every command popped from the queue is the same. If I write queue[1024][0] as queue[1024][1] instead, the command alternates between two distinct commands, and the program crashes in command_thread_main. Apparently it doesn't notice that cmd should be NULL. Changing [1] any higher has no effect as far as I can tell. Any hints?
typedef struct Command {
void (*cb) (char *data, int size);
unsigned char *data;
int size;
} Command;
struct Command queue[1024][0];
int queueEnd = 0;
int queueStart = 0;
static void queue_push(void (*cb), unsigned char *data, int size) {
if (queueEnd >= 1024)
return;
queue[queueEnd]->cb = cb;
queue[queueEnd]->data = data;
queue[queueEnd]->size = size;
queueEnd++;
}
struct Command * queue_pop(void) {
if( queueStart > queueEnd )
return NULL;
return queue[queueStart++];
}
static void *command_thread_main(void *arg) {
struct Command *cmd;
while (!do_exit) {
if(locked) continue;
locked = 1;
cmd = queue_pop();
if(cmd != NULL)
cmd->cb(cmd->data, cmd->size);
}
}