求助,关于编译运内存指令引用错误read不能怎样解决?

shaowenhua86 2009-12-02 11:52:05
调试时出现提示框:“0x00404556”指令引用的“0x00000000”内存。该内存不能为“read”

程序是vc++深入详解视频第15课时的
#include<windows.h>
#include<iostream.h>

DWORD WINAPI Fun1Proc(
LPVOID lpParameter // thread data
);
DWORD WINAPI Fun2Proc(
LPVOID lpParameter // thread data
);
int index=0;
int tickets=100;
void main()
{
HANDLE hThread1,hThread2;
hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
hThread2=CreateThread(NULL,0,Fun2Proc,NULL,0,NULL);
CloseHandle(hThread1);
CloseHandle(hThread2);
/*while(index++<100)
cout<<"main thread is runing"<<endl;*/
//Sleep(10);
Sleep(4000);
}



DWORD WINAPI Fun1Proc(
LPVOID lpParameter // thread data
)
{
/*while(index++<100)
cout<<"thread1 is runing"<<endl;*/
while(1)
{
if(tickets>0)
cout<<"thread1 sell ticket:"<<tickets--<<endl;
else
break;
}
return 0;
}


DWORD WINAPI Fun2Proc(
LPVOID lpParameter // thread data
)
{
while(1)
{
if(tickets>0)
cout<<"thread2 sell ticket:"<<tickets--<<endl;
else
break;
}

return 0;
}
...全文
47 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
老邓 2009-12-03
  • 打赏
  • 举报
回复
给你加了个锁,你跑跑看:

#include <windows.h>

class CriticalSection
{
public:
CriticalSection() { ::InitializeCriticalSection(&m_cs); }
virtual ~CriticalSection() { ::DeleteCriticalSection(&m_cs); }
void Lock() { ::EnterCriticalSection(&m_cs); }
void Unlock() { ::LeaveCriticalSection(&m_cs); }
operator PCRITICAL_SECTION() { return &m_cs; }

protected:
CRITICAL_SECTION m_cs;
};

class Locker
{
public:
Locker(PCRITICAL_SECTION cs) : m_p(cs) { ::EnterCriticalSection(m_p); }
~Locker() { ::LeaveCriticalSection(m_p); }

protected:
PCRITICAL_SECTION m_p;
};

CriticalSection cs;

DWORD WINAPI Fun1Proc(LPVOID lpParameter // thread data
);
DWORD WINAPI Fun2Proc(LPVOID lpParameter // thread data
);
int index = 0;
int tickets = 100;
void main()
{
HANDLE hThread1, hThread2;
hThread1 = CreateThread(NULL, 0, Fun1Proc, NULL, 0, NULL);
hThread2 = CreateThread(NULL, 0, Fun2Proc, NULL, 0, NULL);
CloseHandle(hThread1);
CloseHandle(hThread2);
/*while(index++ <100)
cout < <"main thread is runing" < <endl;*/
//Sleep(10);
Sleep(4000);
}

DWORD WINAPI Fun1Proc(LPVOID lpParameter // thread data
)
{
/*while(index++ <100)
cout < <"thread1 is runing" < <endl;*/
while (1)
{
Locker lock(cs);
if (tickets > 0) cout << "thread1 sell ticket:" << tickets-- << endl;
else break;
}
return 0;
}

DWORD WINAPI Fun2Proc(LPVOID lpParameter // thread data
)
{
while (1)
{
Locker lock(cs);
if (tickets > 0) cout << "thread2 sell ticket:" << tickets-- << endl;
else break;
}

return 0;
}
老邓 2009-12-03
  • 打赏
  • 举报
回复
没崩溃啊。
VC10
#include <iostream>
#include <string>

using namespace std;

#include <windows.h>

DWORD WINAPI Fun1Proc(LPVOID lpParameter // thread data
);
DWORD WINAPI Fun2Proc(LPVOID lpParameter // thread data
);
int index = 0;
int tickets = 100;
void main()
{
HANDLE hThread1, hThread2;
hThread1 = CreateThread(NULL, 0, Fun1Proc, NULL, 0, NULL);
hThread2 = CreateThread(NULL, 0, Fun2Proc, NULL, 0, NULL);
CloseHandle(hThread1);
CloseHandle(hThread2);
/*while(index++ <100)
cout < <"main thread is runing" < <endl;*/
//Sleep(10);
Sleep(4000);
}

DWORD WINAPI Fun1Proc(LPVOID lpParameter // thread data
)
{
/*while(index++ <100)
cout < <"thread1 is runing" < <endl;*/
while (1)
{
if (tickets > 0) cout << "thread1 sell ticket:" << tickets-- << endl;
else break;
}
return 0;
}

DWORD WINAPI Fun2Proc(LPVOID lpParameter // thread data
)
{
while (1)
{
if (tickets > 0) cout << "thread2 sell ticket:" << tickets-- << endl;
else break;
}

return 0;
}
jackzhhuang 2009-12-02
  • 打赏
  • 举报
回复
在哪里崩??

tickets没有做同步访问,很可能自减还没操作完成就被打断。

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧