void* stream_read_thread(void* args) {
    uint32_t buf[1024];
    PicoDrv *pico = ((read_thread_args*)args)->pico;
    int stream = ((read_thread_args*)args)->stream;
    uint32_t * results_buf = ((read_thread_args*)args)->results_buf;
    int num_queries = ((read_thread_args*)args)->num_queries;
    int engine_id = ((read_thread_args*)args)->engine_id;
    uint32_t* query_ids = ((read_thread_args*)args)->query_ids;


fprintf(stderr, "In stream read thread\n");
    int offset = 0;
    uint32_t temp_buf[1024];
    int num_queries_done = 0;
    while (num_queries_done != num_queries) {
        int num_bytes_available = pico->GetBytesAvailable(stream, true);
        if (num_bytes_available >= 16) {
            int num_bytes_to_read = num_bytes_available > 4096 ? 4096 : (num_bytes_available/16)*16;
            // Read full 128-bit packets
            pico->ReadStream(stream, temp_buf, num_bytes_to_read);
fprintf(stderr, "Read something\n");
            // For each result location read
            for (int i = 0; i < num_bytes_to_read / 16; i++) {
                results_buf[offset*2] = temp_buf[i*4];
                results_buf[offset*2 + 1] = temp_buf[i*4 + 1];
                if (results_buf[offset*2] == EOQ) {
                    num_queries_done++;
                }
                offset++;
            }
        }
    }
}   
void* stream_read_thread(void* args) {
    uint32_t buf[1024];
    PicoDrv *pico = ((read_thread_args*)args)->pico;
    int stream = ((read_thread_args*)args)->stream;
    uint32_t * results_buf = ((read_thread_args*)args)->results_buf;
    int num_queries = ((read_thread_args*)args)->num_queries;
    int engine_id = ((read_thread_args*)args)->engine_id;
    uint32_t * num_bytes_returned = ((read_thread_args*)args)->num_bytes_returned;

    int offset = 0;
    uint32_t temp_buf[1024];
    int num_queries_done = 0;
    uint32_t num_bytes_read = 0;
    //while (num_queries_done != num_queries) {
    while (num_queries_done != 1) {
        int num_bytes_available = pico->GetBytesAvailable(stream, true);
        if (num_bytes_available >= 16) {
            int num_bytes_to_read = num_bytes_available > 4096 ? 4096 : (num_bytes_available/16)*16;
            // Read full 128-bit packets
            pico->ReadStream(stream, temp_buf, num_bytes_to_read);
            num_bytes_read += num_bytes_to_read;
            // For each result location read
            for (int i = 0; i < num_bytes_to_read / 16; i++) {
                results_buf[offset*2] = temp_buf[i*4];
                results_buf[offset*2 + 1] = temp_buf[i*4 + 1];
                if (results_buf[offset*2] == EOQ) {
                    num_queries_done++;
                }
                offset++;
                offset%=(RESULT_BUF_SIZE/2);  
            }
        }
    }
    *num_bytes_returned = num_bytes_read;
//    fprintf(stderr, "Read %i bytes\n", num_bytes_read);
}