HI ALL: I have a program: it will create 10 threads to read and write 10
file at same time. You can see the attachment that include the source code.
Sometimes ,it work will.But , it alway report "section error". Thank you for
any advice. My enviroment: linux ubuntu 9.0.
HDF:hdf5-1.8.5-linux-staticHdf-forum is for HDF software users discussion.
[email protected]
http://mail.hdfgroup.org/mailman/listinfo/hdf-forum_hdfgroup.org
#include "hdf5.h"
#include "hdf5_hl.h"
#include <iostream>
#include <pthread.h>
#include <cstring>
#define FILE "file.h5"
using namespace std;
class CZHDF
{
public:
CZHDF()
{
cout<<"CZHDF()"<<endl;
m_fileId=-1;
m_error=0;
}
~CZHDF()
{
if(m_fileId!=-1)
{
close();
}
m_error=0;
m_fileId=-1;
}
int open(char *strFilename,bool bRead=false,bool bOverWrite=false)
{
if(strFilename==NULL)
{
m_fileId=-1;
m_error=-1;
return -1;
}
cout<<"open strFileName="<<strFilename<<endl;
m_fileId=H5Fcreate(strFilename,H5F_ACC_TRUNC,H5P_DEFAULT,H5P_DEFAULT);
if(m_fileId==-1)
{
m_error=-1;
return -1;
}
cout<<"open success m_fileid="<<m_fileId<<endl;
return 0;
}
int WriteEmptyDataset()
{
if(m_fileId==-1)
{
cout<<"WriteEmptyDataset Param error"<<endl;
return -1;
}
hid_t dataset_id, dataspace_id; /* identifiers */
hsize_t dims[2];
int status=0;
/* Create the data space for the dataset. */
dims[0]=4;
dims[1]=6;
dataspace_id = H5Screate_simple(2, dims, NULL);
/* Create the dataset. */
dataset_id = H5Dcreate(m_fileId, "/dset", H5T_STD_I32BE, dataspace_id,
H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
/* End access to the dataset and release resources used by it. */
status = H5Dclose(dataset_id);
/* Terminate access to the data space. */
status = H5Sclose(dataspace_id);
return status;
}
int RWDataSet()
{
int i, j, dset_data[4][6];
int status=0;
/* Initialize the dataset. */
for (i = 0; i < 4; i++)
for (j = 0; j < 6; j++)
dset_data[i][j] = i * 6 + j + 1;
int dataset_id =-1;
dataset_id=H5Dopen(m_fileId, "/dset", H5P_DEFAULT);
if(dataset_id==-1)
{
cout<<"RWDataSet H5DOpen Err ="<<dataset_id<<endl;
return -1;
}else
{
cout<<"RwDataSet dataset_id="<<dataset_id<<endl;
}
/* Write the dataset. */
status = H5Dwrite(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT,dset_data);
/*
for (i = 0; i < 4; i++)
for (j = 0; j < 6; j++)
dset_data[i][j]=0 ;
*/
status = H5Dread(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT,dset_data);
/* Close the dataset. */
status = H5Dclose(dataset_id);
/*
for (i = 0; i < 4; i++)
for (j = 0; j < 6; j++)
cout<<dset_data[i][j] ;
*/
cout<<"RWDataSet end\n"<<endl;
return status;
}
int close()
{
cout<<"close m_fileId="<<m_fileId<<endl;
if(m_fileId!=-1)
m_error=H5Fclose(m_fileId);
m_fileId=-1;
return m_error;
}
private:
hid_t m_fileId;
herr_t m_error;
};
void* produceHDF(void* para)
{
char *strFileName=(char*)para;
CZHDF tmp;
tmp.open(strFileName);
sleep(10);
cout<<"begin produceHDF="<<strFileName<<endl;
tmp.WriteEmptyDataset();
tmp.RWDataSet();
tmp.close();
cout<<"end produceHDF"<<endl;
return NULL;
}
#define THREADNUM 10
int main()
{
/*
//åæä»¶æå¼
int status=0;
CZHDF tmp;
char filename[]="file.h5";
tmp.open(filename);
status=tmp.close();
*/
pthread_t th_ary[100];
memset(th_ary,0,sizeof(pthread_t)*100);
void *retval;
int j=0;
char f1[10][10]={"aaa.h5","bbb.h5","ccc.h5","ddd.h5","eee.h5",
"fff.h5","ggg.h5","hhh.h5","iii.h5","jjj.h5"};
for(int i=0;i<THREADNUM;i++)
{
pthread_create(&th_ary[i],NULL,produceHDF,f1[i]);
}
for(int i=0;i<THREADNUM;i++)
pthread_join(th_ary[i], &retval);
cout<<"main end"<<endl;
return 0;
}
_______________________________________________
Hdf-forum is for HDF software users discussion.
[email protected]
http://mail.hdfgroup.org/mailman/listinfo/hdf-forum_hdfgroup.org