| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 728 人关注过本帖
标题:windows编程遇到的"线程"的问题
只看楼主 加入收藏
wangjw
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-10-27
收藏
 问题点数:0 回复次数:5 
windows编程遇到的"线程"的问题

#include <windows.h>
#include <stdio.h>

class CworkerThread
{
protected:
HANDLE m_hThread;
LPCTSTR m_szName;

public:
CworkerThread(LPCTSTR szName)
{
m_szName=szName;
m_hThread=INVALID_HANDLE_VALUE;

m_hThread=::CreateThread(NULL,0,ThreadProc,reinterpret_cast<LPVOID>(this),0,NULL);
}

virtual ~CworkerThread()
{
::CloseHandle(m_hThread);
}

virtual void WaitForCompletion()
{
::WaitForSingleObject(m_hThread,INFINITE);
}

protected:
static DWORD WINAPI ThreadProc(LPVOID lpParam)
{
CworkerThread *pThis=reinterpret_cast<CworkerThread *>(lpParam);
pThis->DoStuff();
return (0);
}

virtual void DoStuff()    //运行的结果显示这个函数只调用一次,为什么会这样呢?
{
for(int n=0;n<20;++n)
{
printf("Thread: %s\n",m_szName);
printf("ID: %d\n",::GetCurrentThreadId());
printf("count: %d\n\n",n);
}
}
};


void main()
{
CworkerThread wtA("A");
CworkerThread wtB("B");

wtA.WaitForCompletion();
wtB.WaitForCompletion();

printf("Both threads complete.");
}


问题已经用红色字体标识了,期待高手的解答~~~QQ:304635945

搜索更多相关主题的帖子: 线程 windows 
2006-10-27 11:14
corrupt
Rank: 2
等 级:新手上路
威 望:3
帖 子:535
专家分:0
注 册:2004-9-29
收藏
得分:0 

难到不是这样吗??
Thread: A
ID: 1708
count: 0

Thread: A
ID: 1708
count: 1

Thread: A
ID: 1708
count: 2

Thread: A
ID: 1708
count: 3

Thread: A
ID: 1708
count: 4

Thread: A
ID: 1708
count: 5

Thread: A
ID: 1708
count: 6

Thread: A
ID: 1708
count: 7

Thread: A
ID: 1708
count: 8

Thread: A
ID: 1708
count: 9

Thread: A
ID: 1708
count: 10

Thread: A
ID: 1708
count: 11

Thread: A
ID: 1708
count: 12

Thread: A
ID: 1708
count: 13

Thread: A
ID: 1708
count: 14

Thread: A
ID: 1708
count: 15

Thread: A
ID: 1708
count: 16

Thread: A
Thread: B
ID: 2524
count: 0

Thread: B
ID: 2524
count: 1

Thread: B
ID: 2524
count: 2

Thread: B
ID: 2524
count: 3

Thread: B
ID: 2524
count: 4

Thread: B
ID: 2524
count: 5

Thread: B
ID: 2524
count: 6

Thread: B
ID: 2524
count: 7

Thread: B
ID: 2524
count: 8

Thread: B
ID: 2524
count: 9

Thread: B
ID: 2524
count: 10

ID: 1708
count: 17

Thread: A
ID: 1708
count: 18

Thread: A
ID: 1708
count: 19

Thread: B
ID: 2524
count: 11

Thread: B
ID: 2524
count: 12

Thread: B
ID: 2524
count: 13

Thread: B
ID: 2524
count: 14

Thread: B
ID: 2524
count: 15

Thread: B
ID: 2524
count: 16

Thread: B
ID: 2524
count: 17

Thread: B
ID: 2524
count: 18

Thread: B
ID: 2524
count: 19

Both threads complete.Press any key to continue


2006-10-27 13:08
wangjw
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-10-27
收藏
得分:0 

我觉得每个线程这个函数应该执行一次,
这里创建了2个线程,DoStuff()就应该执行2次

而实际上运行显示那个函数只执行了一次,
麻烦你结束一下,谢谢

2006-10-27 15:16
corrupt
Rank: 2
等 级:新手上路
威 望:3
帖 子:535
专家分:0
注 册:2004-9-29
收藏
得分:0 

没错啊..执行了 40次...你可以看结果啊..count都是 两次的.
你可以该成这样子
由于windows是抢占式的,你可以加上同步控制:如下

#include <windows.h>
#include <stdio.h>
CRITICAL_SECTION g_c;
class CworkerThread
{
protected:
HANDLE m_hThread;
LPCTSTR m_szName;

public:
CworkerThread(LPCTSTR szName)
{
m_szName=szName;
m_hThread=INVALID_HANDLE_VALUE;

m_hThread=::CreateThread(NULL,0,ThreadProc,reinterpret_cast<LPVOID>(this),0,NULL);
}

virtual ~CworkerThread()
{
::CloseHandle(m_hThread);
}

virtual void WaitForCompletion()
{
::WaitForSingleObject(m_hThread,INFINITE);
}

protected:
static DWORD WINAPI ThreadProc(LPVOID lpParam)
{
CworkerThread *pThis=reinterpret_cast<CworkerThread *>(lpParam);
pThis->DoStuff();
return (0);
}

virtual void DoStuff()//运行的结果显示这个函数只调用一次,为什么会这样呢?
{EnterCriticalSection(&g_c);
for(int n=0;n<20;++n)
{
printf("Thread: %s\n",m_szName);
printf("ID: %d\n",::GetCurrentThreadId());
printf("count: %d\n\n",n);
}
LeaveCriticalSection(&g_c);
}
};


void main()
{
InitializeCriticalSection(&g_c);
CworkerThread wtA("A");
CworkerThread wtB("B");

wtA.WaitForCompletion();
wtB.WaitForCompletion();
DeleteCriticalSection(&g_c);
printf("Both threads complete.");
}


2006-10-27 15:22
wangjw
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-10-27
收藏
得分:0 

非常感谢你,是我自己粗心了,没看清楚
谢谢你~~~~~~~~~``

2006-10-27 15:34
小海龟
Rank: 6Rank: 6
等 级:贵宾
威 望:23
帖 子:1068
专家分:4
注 册:2006-8-1
收藏
得分:0 
这里 ::WaitForSingleObject(m_hThread,INFINITE);有什么好处?

[bc09] 犯强汉者,虽远比诛!
2007-05-10 14:32
快速回复:windows编程遇到的"线程"的问题
数据加载中...
 
   



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

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