我本来是在dat文件中按定义的线程结构体写入了线程操作,然后用vc中读取文件的方式
读取dat文件就可以执行线程操作,但是现在要求不能用到读取文件的方式,需要在项目里面直接输入线程操作然后执行,应该怎么写啊,求大神!!!
struct ThreadInfo // 定义线程数据结构
{
int serial; // 线程序号
char entity; // 线程类别(判断是读者线程还是写者线程)
double delay; // 线程延迟
double persist; // 线程读写操作持续时间
int node; // 结点位置
int nodenum; // 结点值
};
//读者优先权
void ReaderPriority(char *file)
{
DWORD n_thread = 0; // 线程数目
DWORD thread_ID; // 线程ID
DWORD wait_for_all; // 等待所有线程结束
// 互斥对象
HANDLE h_Mutex;
h_Mutex = CreateMutex(NULL,FALSE,"mutex_for_readcount");
// 线程对象的数组
HANDLE h_Thread[MAX_THREAD_NUM];
ThreadInfo thread_info[MAX_THREAD_NUM];
readcount = 0 ; // 初始化readcountt
InitializeCriticalSection(&RP_Write) ; // 初始化临界区
ifstream inFile; // 打开文件
inFile.open(file) ;
printf("读者优先权:\n\n") ;
while(inFile)
{
//读人每一个读者、写者的信息
inFile>>thread_info[n_thread].serial ;
inFile>>thread_info[n_thread].entity;
inFile>>thread_info[n_thread].delay;
inFile>>thread_info[n_thread].persist;
inFile>>thread_info[n_thread].node;
inFile>>thread_info[n_thread++].nodenum;
inFile.get();
}
for(int i = 0; i < (int)(n_thread); i++)
{
if(thread_info[i].entity == READER_PRINT || thread_info[i].entity == 'p')
{
// 创建读者线程
h_Thread[i] = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(Print_Thread),&thread_info[i],0,&thread_ID);
}
else if (thread_info[i].entity == READER_FIND || thread_info[i].entity == 'f'){
// 创建读者线程
h_Thread[i] = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(Find_Thread),&thread_info[i], 0 ,&thread_ID) ;
}
else if (thread_info[i].entity == WRITER_INSERT || thread_info[i].entity == 't'){
// 创建写者线程
h_Thread[i] = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(Insert_Thread),&thread_info[i], 0 ,&thread_ID) ;
}
else if (thread_info[i].entity == WRITER_DELETE || thread_info[i].entity == 'd'){
// 创建写者线程
h_Thread[i] = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(Delete_Thread),&thread_info[i], 0 ,&thread_ID) ;
}
}
//等待所有线程结束
wait_for_all = WaitForMultipleObjects(n_thread,h_Thread,TRUE,-1);
printf("所有的读者线程和写者线程都已经完成了操作.\n");
}
case 3:
ReaderPriority("thread.dat");
break;