-
-
Save magnusuMET/28a7991db0fcb5392b56573837aa7289 to your computer and use it in GitHub Desktop.
Example of netcdf file that will signal when used with non-threadsafe hdf5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <netcdf.h> | |
#include <assert.h> | |
#include <stdint.h> | |
#include <pthread.h> | |
#define FILENAME "filename.nc" | |
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; | |
static pthread_barrier_t barrier; | |
static void *opener(void* data) { | |
(void) data; | |
int err; | |
int ncid; | |
pthread_mutex_lock(&mutex); | |
err = nc_open(FILENAME, NC_NETCDF4 | NC_NOWRITE, &ncid); | |
pthread_mutex_unlock(&mutex); | |
assert(!err); | |
int varid; | |
pthread_mutex_lock(&mutex); | |
err = nc_inq_varid(ncid, "data", &varid); | |
pthread_mutex_unlock(&mutex); | |
assert(!err); | |
int xtype; | |
int ndims; | |
// Calling this function whilst also calling some other function gives | |
// funnt errors... | |
//pthread_mutex_lock(&mutex); | |
pthread_barrier_wait(&barrier); | |
err = nc_inq_var(ncid, varid, NULL, &xtype, &ndims, NULL, NULL); | |
//pthread_mutex_unlock(&mutex); | |
assert(!err); | |
pthread_mutex_lock(&mutex); | |
err = nc_close(ncid); | |
pthread_mutex_unlock(&mutex); | |
assert(!err); | |
return NULL; | |
} | |
static void *creator(void *data) { | |
(void) data; | |
int err; | |
int ncid; | |
pthread_mutex_lock(&mutex); | |
err = nc_create(FILENAME, NC_NETCDF4 | NC_WRITE | NC_CLOBBER, &ncid); | |
assert(!err); | |
int dimid[2]; | |
err = nc_def_dim(ncid, "x", 3, &dimid[0]); | |
assert(!err); | |
err = nc_def_dim(ncid, "y", 4, &dimid[1]); | |
assert(!err); | |
int varid; | |
err = nc_def_var(ncid, "data", NC_INT, 2, dimid, &varid); | |
assert(!err); | |
err = nc_close(ncid); | |
pthread_mutex_unlock(&mutex); | |
assert(!err); | |
return NULL; | |
} | |
#define N 10 | |
int main() { | |
pthread_barrier_init(&barrier, NULL, N); | |
// nc_initialize(); // Not neccessary, and does not make a difference | |
creator(NULL); | |
pthread_t t[N]; | |
for (int i = 0; i < N; i++) { | |
pthread_create(&t[i], NULL, opener, NULL); | |
} | |
for (int i = 0; i < N; i++) { | |
void *data; | |
pthread_join(t[i], &data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment