你能写个多线程的小程序吗?

Begin__End 2001-08-31 09:44:34
我是个初学者,现在想学习多线程方面的知识,看了些理论,可没有实践。

各位能否写个多线程的小程序(比在对话框中,两个进度条,用两个线程实现)。其它的也可以。

写了烦mail : begin_end@china.com

分一定给。真的很感谢。现在只能给这么多分,以后一定加上。
...全文
197 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
use_id 2001-08-31
  • 打赏
  • 举报
回复
up
use_id 2001-08-31
  • 打赏
  • 举报
回复
言归正传,pitchstar(lsy) 能否帮我传个VC的多线程小例子来。

尹妹儿在上面。

不要那么复杂吧。

小程序对大家来说,该是小菜一碟。再次谢了。
lixuyu 2001-08-31
  • 打赏
  • 举报
回复
up
fmding 2001-08-31
  • 打赏
  • 举报
回复
gz
use_id 2001-08-31
  • 打赏
  • 举报
回复
关注
pitchstar 2001-08-31
  • 打赏
  • 举报
回复
刚才上边的就是啊,我完整拷贝来下来的。。。编译通过
Begin__End 2001-08-31
  • 打赏
  • 举报
回复
言归正传,pitchstar(lsy) 能否帮我传个VC的多线程小例子来。

尹妹儿在上面。
pitchstar 2001-08-31
  • 打赏
  • 举报
回复
抄一段,《windows 核心编程》p134。
start
下面是关于 _beginthreadex 的一些要点:
-- 每个线程均获得由 c/c++ 运行期库的堆栈分配的自己的 tiddata 内存结构。
-- 传递给_beginthreadex 的线程函数地址保存在 tiddata 内存块中。传递给该函数的参数也
保存在该数据块中。
-- _beginthreadex 确实从内部调用了 CreateThread,因为这是操作系统了解如何创建线程
的唯一方法
-- 当调用 CreateThread 是,他被告知通过调用 _threadstartex 而不是 pfnStartAddr
来启动执行新线程。还有,传递给线程的参数是 tiddata 数据结构而不是 pvParam 的地址
-- 如果一切顺利,就会象 CreateThread 那样返回线程句柄,失败 返回 null

end

标准 c 运行期库远远早于线程问世,所以在多线程程序上运行的时候会有一些问题,比如 errno
一个线程设为 1 后正巧被中断,第二个线程这是把 errno 设为 3 ,当第一个线程恢复运行时检查errno 。。。就出问题了。 tiddata 数据结构和 _beginthreadex 就是为解决这个问题产生的。_endthread 则完成对应的清除工作。当然,关于这个问题还有很多内容,我手工输入太慢了。详细可以参考《windows 核心编程》----非常好的一本书
Begin__End 2001-08-31
  • 打赏
  • 举报
回复
pitchstar(lsy) 我现在看得还比较吃力的。

_beginthreadex推存使用的理由。

多谢关注。
pitchstar 2001-08-31
  • 打赏
  • 举报
回复
核心编程这本书上推荐用 _beginthreadex 建立一个线程,用 _endthreadex 退出线程
他们会处理一些 标准 c 库函数的上下文。
Begin__End 2001-08-31
  • 打赏
  • 举报
回复
emailcdh(大灰狼) 要是有这方面的书早看了。来这是没有办法才来的。

qing_zb(micro) 搜索了,都是一个个的问题。这个我不知道它们的来龙去脉的。

所以想找个完整的小程序看看。

多谢两位关注。
pitchstar 2001-08-31
  • 打赏
  • 举报
回复
一个很笨的多线程扫描程序,大家顺便帮忙看看怎么优化
从端口 1 开始 connect ,共起了


#include "winsock2.h"
#include "ws2tcpip.h"
#include "stdlib.h"
#include "stdio.h"
#include "windows.h"

int getport(u_short *);
unsigned long __stdcall scanport(void *);
static int threadcount=0;
int main(){
WSADATA wsd;
DWORD newthread;

if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)
{
::MessageBox(NULL,"socket 失败 ","sorry!",MB_OK);
}
for(int i=0;i<300;i++){
::CreateThread (NULL,0,scanport,NULL,0,&newthread);
threadcount++;
}
while(threadcount)::Sleep(100);
return 0;
}
unsigned long __stdcall scanport(void *){
u_short port;
SOCKET s;
struct sockaddr_in sin;
char portstr[7];

sin.sin_family =AF_INET;
sin.sin_addr.s_addr =inet_addr("192.168.0.1");

while(getport(&port)){
s = socket(AF_INET, SOCK_STREAM, 0);
// itoa(port,portstr,10);
// printf("\n\tport %d:",port);
sin.sin_port =htons(port);
if(!connect(s,(sockaddr *)&sin,sizeof(sin)))printf("\n\ta server is running on port:%d",port);
closesocket(s);
}
threadcount--;
return 0;
}

int getport(u_short * port){
static u_short currentport=1;
currentport++;
// printf("\nport %d",currentport);
if(currentport>65534)//65535
return 0;
else
(*port)=currentport;
return 1;
}
qing_zb 2001-08-31
  • 打赏
  • 举报
回复
多线程,搜索
emailcdh 2001-08-31
  • 打赏
  • 举报
回复
还是找本书吧,有详细的说明!
Begin__End 2001-08-31
  • 打赏
  • 举报
回复
方雨多谢你的up
wzaen 2001-08-31
  • 打赏
  • 举报
回复
up
Begin__End 2001-08-31
  • 打赏
  • 举报
回复
up
use_id 2001-08-31
  • 打赏
  • 举报
回复
找到一个,给你帖:

#include <afxmt.h>
//---------- g_var
UINT ThreadProc(LPVOID pParam);
CEvent g_eBusy;


BOOL CPicFileDlg::OnInitDialog()
{
CFileDialog::OnInitDialog();

// TODO: Add extra initialization here
SetTimer(1, 500, NULL);
g_eBusy.SetEvent();

return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}


UINT ThreadProc(LPVOID pParam)
{
g_eBusy.ResetEvent();

CWnd hWnd;
hWnd.Attach((HWND)pParam);
ASSERT(hWnd);

CRect m_Rect;
CDC *m_pDC;
hWnd.GetClientRect(&m_Rect);
m_pDC=hWnd.GetDC();

PreViewPic(g_sFileName, m_pDC, m_Rect);

hWnd.Detach();

g_eBusy.SetEvent();
return 0;
}


void CPicFileDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CString sName=GetFileName(), sExt=GetFileExt();

if (sName=="" && sExt.CompareNoCase("bmp") && sExt.CompareNoCase("gif") && sExt.CompareNoCase("jpg"))
return;

if (g_sFileName.CompareNoCase(sName) && WaitForSingleObject(g_eBusy, 0)==WAIT_OBJECT_0)
{
g_sFileName=sName;
AfxBeginThread(ThreadProc,m_hPicView.GetSafeHwnd());
}

CFileDialog::OnTimer(nIDEvent);
}

void CPicFileDlg::OnDestroy()
{
// TODO: Add your message handler code here
KillTimer(1);
WaitForSingleObject(g_eBusy, INFINITE);

CFileDialog::OnDestroy();
}
Begin__End 2001-08-31
  • 打赏
  • 举报
回复
还没有人来了吗?

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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