读者与写者问题-fatal error C1004: unexpected end of file found

问题描述

fatal error C1004: unexpected end of file found
//读者与写者问题

#include

#include

#include

#ifdef _MT

//设置控制台输出颜色

BOOL SetConsoleColor(WORD wAttributes)

{

HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

if (hConsole == INVALID_HANDLE_VALUE)

return FALSE;

return SetConsoleTextAttribute(hConsole wAttributes);  

}

const int READER_NUM = 5; //读者个数

//关键段和事件

CRITICAL_SECTION g_cs g_cs_writer_count;

HANDLE g_hEventWriter g_hEventNoReader;

int g_nReaderCount;

//读者线程输出函数(变参函数的实现)

void ReaderPrintf(char *pszFormat ...)

{

va_list pArgList;

va_start(pArgList pszFormat);  EnterCriticalSection(&g_cs);  vfprintf(stdout pszFormat pArgList);  LeaveCriticalSection(&g_cs);  va_end(pArgList);  

}

//读者线程函数

unsigned int __stdcall ReaderThreadFun(PVOID pM)

{

ReaderPrintf("" 编号为%d的读者进入等待中...n"" GetCurrentThreadId());

//等待写者完成

WaitForSingleObject(g_hEventWriter INFINITE);

//读者个数增加  EnterCriticalSection(&g_cs_writer_count);  g_nReaderCount++;  if (g_nReaderCount == 1)      ResetEvent(g_hEventNoReader);  LeaveCriticalSection(&g_cs_writer_count);  //读取文件  ReaderPrintf(""编号为%d的读者开始读取文件...n"" GetCurrentThreadId());  Sleep(rand() % 100);  //结束阅读读者个数减小空位增加  ReaderPrintf("" 编号为%d的读者结束读取文件n"" GetCurrentThreadId());  //读者个数减少  EnterCriticalSection(&g_cs_writer_count);  g_nReaderCount--;  if (g_nReaderCount == 0)      SetEvent(g_hEventNoReader);  LeaveCriticalSection(&g_cs_writer_count);  return 0;  

}

//写者线程输出函数

void WriterPrintf(char *pszStr)

{

EnterCriticalSection(&g_cs);

SetConsoleColor(FOREGROUND_GREEN);

printf("" %sn"" pszStr);

SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);

LeaveCriticalSection(&g_cs);

}

//写者线程函数

unsigned int __stdcall WriterThreadFun(PVOID pM)

{

WriterPrintf(""写者线程进入等待中..."");

//等待读文件的读者为零

WaitForSingleObject(g_hEventNoReader INFINITE);

//标记写者正在写文件

ResetEvent(g_hEventWriter);

//写文件  WriterPrintf(""  写者开始写文件....."");  Sleep(rand() % 100);  WriterPrintf(""  写者结束写文件"");  //标记写者结束写文件  SetEvent(g_hEventWriter);  return 0;  

}

int main()

{
printf("" 读者写者问题n"");

//初始化事件和信号量

InitializeCriticalSection(&g_cs);

InitializeCriticalSection(&g_cs_writer_count);

//手动置位初始已触发

g_hEventWriter = CreateEvent(NULL TRUE TRUE NULL);

g_hEventNoReader = CreateEvent(NULL FALSE TRUE NULL);

g_nReaderCount = 0;

int i;

HANDLE hThread[READER_NUM + 1];

//先启动二个读者线程

for (i = 1; i <= 2; i++)

hThread[i] = (HANDLE)_beginthreadex(NULL 0 ReaderThreadFun NULL 0 NULL);

//启动写者线程

hThread[0] = (HANDLE)_beginthreadex(NULL 0 WriterThreadFun NULL 0 NULL);

Sleep(50);

//最后启动其它读者结程

for ( ; i <= READER_NUM; i++)

hThread[i] = (HANDLE)_beginthreadex(NULL 0 ReaderThreadFun NULL 0 NULL);

WaitForMultipleObjects(READER_NUM + 1 hThread TRUE INFINITE);

for (i = 0; i < READER_NUM + 1; i++)

CloseHandle(hThread[i]);

//销毁事件和信号量

CloseHandle(g_hEventWriter);

CloseHandle(g_hEventNoReader);

DeleteCriticalSection(&g_cs);

DeleteCriticalSection(&g_cs_writer_count);

return 0;

}

解决方案

文件括号等配对了没,分号都有了

时间: 2024-08-03 18:20:19

读者与写者问题-fatal error C1004: unexpected end of file found的相关文章

fatal error C1004: unexpected end of file found

问题描述 fatal error C1004: unexpected end of file found #include #include #include #define N 100 #define n 5 #define m 5 #define w 5 typedef struct { int itemnum; int top; int range[N]; int mark[N]; }itemnode; itemnode t[N]; struct head { itemnode t[N];

关于dll文件的生成以及找不到jni.h的解决办法(fatal error C1083: Cannot open include file: &#039;jni.h&#039;:)

引用 环境:Winxp+VC6.0  过程:编绎生成Dll文件时出错 首先是关于dll文件的生成:  方法如下:  打开VC++6.0,新建->; 工程 ->; win32 Dynamic-Link Library在向导中选择空工程  添加如用javah生成的头文件和自己写的一个cpp文件,然后编译运行,如果没有出现错误,既可以在编译成功的debug文件夹中找到dll文件,如果出现找不到jni.h的错误,那么用如下方法解决:  fatal error C1083: Cannot open in

Linux 有问必答:如何在Linux 中修复“fatal error: x264.h: No such file or directo

Linux 有问必答:如何在Linux 中修复"fatal error: x264.h: No such file or directo 提问: 我想在Linux中从源码编译视频编码程序.到那时,在编译时,我遇到了一个错误"fatal error: x264.h: No such file or directory",我该如何修复? 下面的编译错误错明你系统中没有x264开发库文件. fatal error: x264.h: No such file or directory

win7 64 安装mysql-python:_mysql.c(42) : fatal error C1083: Cannot open include file: &#039;config-win.h&#039;: No such file or directory

今天想在在win7 64位环境下使用python 操作mysql 在安装MySQL-python 时报错:   _mysql.c _mysql.c(42) : fatal error C1083: Cannot open include file: 'config-win.h': No s uch file or directory     error: command '"C:\Users\fnngj\AppData\Local\Programs\Common\Microsoft\Visual

C++求解 fatal error C1004: 发现意外的文件尾

问题描述 //表达式的计算#include"stdafx.h"#include"iostream"#include"stack"#include"map"usingnamespacestd;classinfix2postfix{private:stringinfix;//用于转换的中缀表达式stringpostfix;//后缀表达式stack<string>stk;//用于存储运算符的堆栈map<string

fatal error C1010: 在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include &quot;stdafx.h

错误描述:fatal error C1010: 在查找预编译头时遇到意外的文件结尾.是否忘记了向源中添加"#include "stdafx.h""? 错误分析:     此错误发生的原因是编译器在寻找预编译指示头文件(默认#include "stdafx.h")时,文件未预期结束.没有找到预编译指示信息的头文件"stdafx.h".     (因为工程中的每个cpp文件属性默认都是使用预编译头(/YU)的,但是添加的第三方文件

VC6的fatal error C1010解决办法

在编译VC++6.0时,有时候会出现fatal error C1010: unexpected end of file while looking for precompiled header directive 的错误. 解决方法: 1.如果发生错误的文件是由其他的C代码文件添加进入当前工程而引起的,则Alt+F7进入当前工程的 Settings,选择C/C++选项卡,从Category组合框中选中Precompiled Headers,选择Not Using Precompiled head

升级mysql_upgrade 提示错误FATAL ERROR: Upgrade failed的原因

升级mysql_upgrade 提示错误FATAL ERROR: Upgrade failed的原因 2014年07月25日 10:33 by:老修--走失的镜头盖分类:网站技术 -> mysql 正文我来说两句 用mysql_upgrade 升级mysql的两种错误: 第一种. 提示FATAL ERROR:Upgrade failed 的时候时候输入mysql升级命令并输入密码后出现下面的错误提示: [root@AY1405111111157597d01Z ~]# mysql_upgrade

gcc-fatal error: stdio. h :no such file or directory

问题描述 fatal error: stdio. h :no such file or directory 今天刚装的Linux mint,用gcc编译程序不通过,出现 fatal error: stdio.h :no such file and directory的build messages,下载codeblocks也是出现这样的错误,还望高手指点. 解决方案 In file included from scripts/kconfig/lxdialog/checklist.c:24: scr