| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1030 人关注过本帖
标题:线程同步中怎样添加线程控制函数啊?
只看楼主 加入收藏
longfengyuan
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2007-1-10
收藏
 问题点数:0 回复次数:4 
线程同步中怎样添加线程控制函数啊?

书上说把这段代码直接加在ch1view.cpp
#include"afxmt.h"
int m_count;
int m_data[5];
CCriticalSection critical;
//////////////////////////////////////////////////////////////////////////
class ch1View : public CView
{ UINT MyFunction1(LPVOID param)

{
critical.Lock();
for (int i=0;i<5;i++)
{
m_count++;
::Sleep(200);
m_data[i]=m_count;
}
critical.Unlock();
CString str;
str.Format("%d-%d-%d-%-%d",m_data[0],m_data[1],m_data[2],m_data[3],m_data[4]);
MessageBox((HWND) param,str,"1",MB_OK);
return 0;
}
UINT MyFunction2(LPVOID param)
{
for (int i=0;i<5;i++)
{
m_count++;
::Sleep(150);
m_data[i]=m_count;
}
CString str;
str.Format("%d-%d-%d-%d-%d",m_data[0],m_data[1],m_data[2],m_data[3],m_data[4]);
MessageBox((HWND)param,str,"2",MB_OK);
return 0;
}
是这样直接在view中添加吗?
//////////////////////////////////////////////////////////////////////////
下面是两个菜单处理程序
void CCh7_2View::OnCom()
{
// TODO: Add your command handler code here
m_count=0;
HWND hWnd=GetSafeHwnd();
AfxBeginThread(MyFunction2,hWnd);
AfxBeginThread(MyFunction2,hWnd);
}

void CCh7_2View::OnCri()
{
// TODO: Add your command handler code here
m_count=0;
HWND hWnd=GetSafeHwnd();
AfxBeginThread(MyFunction1,hWnd);
AfxBeginThread(MyFunction1,hWnd);
}
结果出现了很多错误:
error C2065: 'critical' : undeclared identifier
d:\ch72\ch72view.h(25) : error C2228: left of '.Lock' must have class/struct/union type
d:\ch72\ch72view.h(28) : error C2065: 'm_count' : undeclared identifier
d:\ch72\ch72view.h(30) : error C2065: 'm_data' : undeclared identifier
d:\ch72\ch72view.h(30) : error C2109: subscript requires array or pointer type
d:\ch72\ch72view.h(30) : error C2106: '=' : left operand must be l-value
d:\ch72\ch72view.h(32) : error C2228: left of '.Unlock' must have class/struct/union type
d:\ch72\ch72view.h(34) : error C2109: subscript requires array or pointer type
d:\ch72\ch72view.h(34) : error C2109: subscript requires array or pointer type
d:\ch72\ch72view.h(34) : error C2109: subscript requires array or pointer type
d:\ch72\ch72view.h(34) : error C2109: subscript requires array or pointer type
d:\ch72\ch72view.h(34) : error C2109: subscript requires array or pointer type
d:\ch72\ch72view.h(35) : error C2660: 'MessageBoxA' : function does not take 4 parameters
d:\ch72\ch72view.h(44) : error C2109: subscript requires array or pointer type
d:\ch72\ch72view.h(44) : error C2106: '=' : left operand must be l-value
d:\ch72\ch72view.h(47) : error C2109: subscript requires array or pointer type
d:\ch72\ch72view.h(47) : error C2109: subscript requires array or pointer type
d:\ch72\ch72view.h(47) : error C2109: subscript requires array or pointer type
d:\ch72\ch72view.h(47) : error C2109: subscript requires array or pointer type
d:\ch72\ch72view.h(47) : error C2109: subscript requires array or pointer type
d:\ch72\ch72view.h(48) : error C2660: 'MessageBoxA' : function does not take 4 parameters
MainFrm.cpp
ch72Doc.cpp
ch72View.cpp
d:\ch72\ch72view.h(25) : error C2065: 'critical' : undeclared identifier
d:\ch72\ch72view.h(25) : error C2228: left of '.Lock' must have class/struct/union type
d:\ch72\ch72view.h(28) : error C2065: 'm_count' : undeclared identifier
d:\ch72\ch72view.h(30) : error C2065: 'm_data' : undeclared identifier
d:\ch72\ch72view.h(30) : error C2109: subscript requires array or pointer type
d:\ch72\ch72view.h(30) : error C2106: '=' : left operand must be l-value
d:\ch72\ch72view.h(32) : error C2228: left of '.Unlock' must have class/struct/union type
d:\ch72\ch72view.h(34) : error C2109: subscript requires array or pointer type
d:\ch72\ch72view.h(34) : error C2109: subscript requires array or pointer type
d:\ch72\ch72view.h(34) : error C2109: subscript requires array or pointer type
d:\ch72\ch72view.h(34) : error C2109: subscript requires array or pointer type
d:\ch72\ch72view.h(34) : error C2109: subscript requires array or pointer type
d:\ch72\ch72view.h(35) : error C2660: 'MessageBoxA' : function does not take 4 parameters
d:\ch72\ch72view.h(44) : error C2109: subscript requires array or pointer type
d:\ch72\ch72view.h(44) : error C2106: '=' : left operand must be l-value
d:\ch72\ch72view.h(47) : error C2109: subscript requires array or pointer type
d:\ch72\ch72view.h(47) : error C2109: subscript requires array or pointer type
d:\ch72\ch72view.h(47) : error C2109: subscript requires array or pointer type
d:\ch72\ch72view.h(47) : error C2109: subscript requires array or pointer type
d:\ch72\ch72view.h(47) : error C2109: subscript requires array or pointer type
d:\ch72\ch72view.h(48) : error C2660: 'MessageBoxA' : function does not take 4 parameters
D:\ch72\ch72View.cpp(11) : error C2040: 'm_data' : 'int [5]' differs in levels of indirection from 'int'
D:\ch72\ch72View.cpp(12) : error C2371: 'critical' : redefinition; different basic types
D:\ch72\ch72View.cpp(115) : error C2665: 'AfxBeginThread' : none of the 2 overloads can convert parameter 1 from type 'unsigned int (void *)'
D:\ch72\ch72View.cpp(116) : error C2665: 'AfxBeginThread' : none of the 2 overloads can convert parameter 1 from type 'unsigned int (void *)'
D:\ch72\ch72View.cpp(124) : error C2665: 'AfxBeginThread' : none of the 2 overloads can convert parameter 1 from type 'unsigned int (void *)'
请大侠们指教,
先谢谢!

搜索更多相关主题的帖子: 线程 函数 
2007-01-10 17:59
cz522321
Rank: 1
等 级:禁止访问
威 望:2
帖 子:569
专家分:5
注 册:2006-3-13
收藏
得分:0 

#include"afxmt.h"
int m_count;
int m_data[5];
CCriticalSection critical;
//////////////////////////////////////////////////////////////////////////
ch1View::ch1View()
{ }


UINT MyFunction1(LPVOID param)

{
critical.Lock();
for (int i=0;i<5;i++)
{
m_count++;
::Sleep(200);
m_data[i]=m_count;
}
critical.Unlock();
CString str;
str.Format("%d-%d-%d-%-%d",m_data[0],m_data[1],m_data[2],m_data[3],m_data[4]);
MessageBox((HWND) param,str,"1",MB_OK);
return 0;
}


UINT MyFunction2(LPVOID param)
{
for (int i=0;i<5;i++)
{
m_count++;
::Sleep(150);
m_data[i]=m_count;
}
CString str;
str.Format("%d-%d-%d-%d-%d",m_data[0],m_data[1],m_data[2],m_data[3],m_data[4]);
MessageBox((HWND)param,str,"2",MB_OK);
return 0;
}

函数加在外面。


2007-01-10 21:16
longfengyuan
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2007-1-10
收藏
得分:0 

但是还是有错误,运行的时候出现这个错误:
Loaded symbols for 'D:\ch72\Debug\ch72.exe'
Loaded 'C:\WINNT\system32\NTDLL.DLL', no matching symbolic information found.
Loaded symbols for 'C:\WINNT\system32\MFC42D.DLL'
Loaded symbols for 'C:\WINNT\system32\MSVCRTD.DLL'
Loaded 'C:\WINNT\system32\KERNEL32.DLL', no matching symbolic information found.
Loaded 'C:\WINNT\system32\GDI32.DLL', no matching symbolic information found.
Loaded 'C:\WINNT\system32\user32.dll', no matching symbolic information found.
Loaded symbols for 'C:\WINNT\system32\MFCO42D.DLL'
Loaded 'C:\WINNT\system32\imm32.dll', no matching symbolic information found.
Loaded 'C:\WINNT\system32\advapi32.dll', no matching symbolic information found.
Loaded 'C:\WINNT\system32\rpcrt4.dll', no matching symbolic information found.
Loaded 'C:\WINNT\system32\lpk.dll', no matching symbolic information found.
Loaded 'C:\WINNT\system32\usp10.dll', no matching symbolic information found.
Loaded 'C:\WINNT\system32\mfc42loc.dll', no matching symbolic information found.
Loaded 'D:\Windows木马清道夫 8.8上网必备绿色注册可升级版\Windows木马清道夫 8.8上网必备绿色注册可升级版\ftcsetup\Filehook.dll', no matching symbolic information found.
Loaded 'C:\WINNT\system32\oleaut32.dll', no matching symbolic information found.
Loaded 'C:\WINNT\system32\OLE32.DLL', no matching symbolic information found.
Loaded 'C:\WINNT\system32\indicdll.dll', no matching symbolic information found.
Loaded 'C:\WINNT\system32\comctl32.dll', no matching symbolic information found.
The thread 0x5D4 has exited with code 0 (0x0).
The program 'D:\ch72\Debug\ch72.exe' has exited with code 0 (0x0).

2007-01-11 15:47
cz522321
Rank: 1
等 级:禁止访问
威 望:2
帖 子:569
专家分:5
注 册:2006-3-13
收藏
得分:0 

代码中的:
void CCh7_2View::OnCom()
{
// TODO: Add your command handler code here
m_count=0;
HWND hWnd=GetSafeHwnd();
AfxBeginThread(MyFunction2,hWnd);
AfxBeginThread(MyFunction2,hWnd);
}

void CCh7_2View::OnCri()
{
// TODO: Add your command handler code here
m_count=0;
HWND hWnd=GetSafeHwnd();
AfxBeginThread(MyFunction1,hWnd);
AfxBeginThread(MyFunction1,hWnd);
}

改为:
void CCh7_2View::OnCom()
{
// TODO: Add your command handler code here
m_count=0;
HWND hWnd=GetSafeHwnd();
AfxBeginThread(MyFunction2,hWnd);
//AfxBeginThread(MyFunction2,hWnd);
}

void CCh7_2View::OnCri()
{
// TODO: Add your command handler code here
m_count=0;
HWND hWnd=GetSafeHwnd();
AfxBeginThread(MyFunction1,hWnd);
//AfxBeginThread(MyFunction1,hWnd);
}
把红色部分注释掉,或者删除。


2007-01-11 16:56
longfengyuan
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2007-1-10
收藏
得分:0 

怎么发送图片呢?我这里编译连接都能通过,就是运行的时候出现exception的现象!,谢谢您的帮助!

2007-01-12 09:27
快速回复:线程同步中怎样添加线程控制函数啊?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026084 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved