多线程
源代码
#include
#include
#include
using namespace std;
// 多线程之pthread_create的参数结构体
typedef struct
{
int myid;
double *matrix;
char name[1024];
}THREADPARAM, *PTHREADPARAM;
// 多线程之std::thread的测试函数
void func(int myid)
{
std::cout << "this is func!" << endl;
// 打印线程ID
std::cout << "myid is : " << myid << " this thread id is " << std::this_thread::get_id() << endl;
}
// 多线程之pthread_create的测试函数,返回值为void*,输入参数的结构体为void*
void* func2(void* pthreadparam)
{
// 解析参数
std::cout << "this is func2! " << "thread id is : " << ((PTHREADPARAM)pthreadparam)->myid <
编译
g++ -o thread-l.out -std=c++11 -lpthread thread-l.cpp
读写文件
源代码
#include
#include
using namespace std;
int WriteData2File(char *filename, double *matrix, int row, int col)
{
// 错误检查
if (filename == NULL)
{
printf("Null Pointer for filename! n");
return 0x100003;
}
if (matrix == NULL)
{
printf("Null Pointer for Matrix! n");
return 0x100002;
}
if (row < 1 || col < 1)
{
printf("Illegal Matrix Row and Column! n");
return 0x100001;
}
FILE *wFile;
// 打开文件
wFile = fopen(filename, "wb");
// 错误检查
if (wFile == NULL)
{
printf("Error Opening the File for Null Pointer! n");
return 0x100004;
}
else
{
// 写入文件
fwrite(matrix, sizeof(double), row * col, wFile);
// 关闭文件
fclose(wFile);
return 0;
}
}
int ReadDataFromFile(char *filename, double *matrix, int row, int col)
{
// 错误检查
if (filename == NULL)
{
printf("Null Pointer for filename! n");
return 0x100003;
}
if (matrix == NULL)
{
printf("Null Pointer for Matrix! n");
return 0x100002;
}
if (row < 1 || col < 1)
{
printf("Illegal Matrix Row and Column! n");
return 0x100001;
}
FILE *rFile;
// 打开文件
rFile = fopen(filename, "r");
// 错误检查
if (rFile == NULL)
{
printf("Error Opening the File for Null Pointer! n");
return 0x100004;
}
else
{
//查找文件的开头
fseek(rFile, 0, SEEK_SET);
//读取文件
fread(matrix, sizeof(double), row * col, rFile);
// 关闭文件
fclose(rFile);
return 0;
}
}
int main()
{
int iErr;
const int COL = 10;
const int ROW = 10;
char filename[1024];
// 写入文件名
sprintf(filename, "testdata/matrix%04lld.a", 1);
cout << "File Name is : " << filename <
编译
g++ writefile-l.cpp -o writefile-l.out