#include <stdio.h>
#include <stdlib.h>
#include <slow5/slow5.h>
#include <pthread.h>
#include <errno.h>
#define FILE_PATH "examples/example.slow5"
#define NUM_THREADS 5
#define READ_BUFFER_SIZE 1024
typedef struct {
slow5_file_t *sp;
char *mem;
size_t bytes;
} pthread_arg;
void* access_read(void* voidargs) {
pthread_arg *args = (pthread_arg*)voidargs; slow5_rec_t *rec = NULL;
int ret = slow5_decode(&args->mem, &args->bytes, &rec, args->sp); if (ret < 0) {
fprintf(stderr, "Error in when decoding the read\n");
} else {
fprintf(stderr, "Successfully decoded the read %s with %ld raw signal samples\n", rec->read_id, rec->len_raw_signal);
}
slow5_rec_free(rec);
free(args->mem);
pthread_exit(0);
}
int main(){
slow5_file_t *sp = slow5_open(FILE_PATH,"r");
if(sp==NULL){
fprintf(stderr,"Error in opening file\n");
exit(EXIT_FAILURE);
}
int ret,t = 0;
char *mem[READ_BUFFER_SIZE] = {NULL}; size_t bytes[READ_BUFFER_SIZE] = {0};
int n_rec = 0;
for(n_rec=0;n_rec<READ_BUFFER_SIZE;n_rec++){
if(slow5_get_next_bytes(&mem[n_rec],&bytes[n_rec],sp)<0){
if (slow5_errno != SLOW5_ERR_EOF) { fprintf(stderr, "Error reading from SLOW5 file %d", slow5_errno);
exit(EXIT_FAILURE);
} else {
break;
}
}
}
fprintf(stderr, "Read %d raw records\n", n_rec);
pthread_t tids[NUM_THREADS];
pthread_arg args[NUM_THREADS];
for(t = 0; t < NUM_THREADS; t++){
args[t].sp = sp; args[t].mem = mem[t]; args[t].bytes = bytes[t];
ret = pthread_create(&tids[t], NULL, access_read, (void*)(&args[t]));
if(ret < 0){
fprintf(stderr, "Error creating thread\n");
exit(EXIT_FAILURE);
}
}
for (t = 0; t < NUM_THREADS; t++) {
int ret = pthread_join(tids[t], NULL);
if(ret < 0){
fprintf(stderr, "Error creating thread\n");
exit(EXIT_FAILURE);
}
}
slow5_close(sp);
return 0;
}