Thread编程求助

hehemouse 2000-03-05 04:47:00
Thread编程求助
各位大侠,本人刚开始学Thread编程,有些问题想请教大家
1.我想把一段函数做成线程,比如叫CMyThread
在程序中每次只能同时运行5个这样的线程,其中一个做完
以后,才可以开始第六个线程。是不是应该用CSemaphore?
怎么用呢?能不能给比较详细的例子?
2.等所有这样的CMyThread都做完以后,我才可以开始做
其他的函数,这个要求又怎么完成?
...全文
201 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
fupf88 2000-03-05
  • 打赏
  • 举报
回复
除了WaitForSingleObject,你还可以试以下两种方法:
1 将线程做成exe,用System("path\aaa.exe"),这种方法和WaitForSingleObject
一样使主线程停下来,程序处于不响应状态,而且还出现dos窗口,不太好
2 在线程结束时,SendMessage给主线程,执行下一个任务
garden 2000-03-05
  • 打赏
  • 举报
回复
/* 洷咡涴跺嗣昢煦饜腔瞰赽夔賤樵斕腔恀枙 */
/*
* TaskQueM.c
*
* Sample code for "Multithreading Applications in Win32"
* This is from Chapter 3, Listing 3-3
*
* Call ThreadFunc NUM_TASKS times, using
* no more than THREAD_POOL_SIZE threads.
* This version uses WaitForMultipleObjects
* to provide a more optimal solution.
*
* Build command: cl /MD TaskQueM.c
*/

#define WIN32_LEAN_AND_MEAN
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "MtVerify.h"

DWORD WINAPI ThreadFunc(LPVOID);

#define THREAD_POOL_SIZE 3
#define MAX_THREAD_INDEX THREAD_POOL_SIZE-1
#define NUM_TASKS 6

int main()
{
HANDLE hThrds[THREAD_POOL_SIZE];
int slot = 0;
DWORD threadId;
int i;
DWORD rc;

for (i=1; i<=NUM_TASKS; i++)
{
/* Until we've used all threads in *
* the pool, do not need to wait *
* for one to exit */
if (i > THREAD_POOL_SIZE)
{
/* Wait for one thread to terminate */
rc = WaitForMultipleObjects(
THREAD_POOL_SIZE,
hThrds,
FALSE,
INFINITE );
slot = rc - WAIT_OBJECT_0;
MTVERIFY( slot >= 0
&& slot < THREAD_POOL_SIZE );
printf("Slot %d terminated\n", slot );
MTVERIFY( CloseHandle(hThrds[slot]) );
}
/* Create a new thread in the given
* available slot */
MTVERIFY( hThrds[slot++] = CreateThread(NULL,
0,
ThreadFunc,
(LPVOID)slot,
0,
&threadId ) );
printf("Launched thread #%d (slot %d)\n", i, slot);
}

/* Now wait for all threads to terminate */
rc = WaitForMultipleObjects(
THREAD_POOL_SIZE,
hThrds,
TRUE,
INFINITE );
MTVERIFY( rc >= WAIT_OBJECT_0
&& rc < WAIT_OBJECT_0+THREAD_POOL_SIZE);
for (slot=0; slot<THREAD_POOL_SIZE; slot++)
MTVERIFY( CloseHandle(hThrds[slot]) );
printf("All slots terminated\n");

return EXIT_SUCCESS;
}

/*
* This function just calls Sleep for
* a random amount of time, thereby
* simulating some task that takes time.
*
* The param "n" is the index into
* the handle array, kept for informational
* purposes.
*/
DWORD WINAPI ThreadFunc(LPVOID n)
{
srand( GetTickCount() );

Sleep((rand()%10)*800+500);
printf("Slot %d idle\n", n);
return ((DWORD)n);
}
茂奇软件 2000-03-05
  • 打赏
  • 举报
回复
I think it is thread pool.
You can create 5 threads at first.
each one wait the event and begin his service.
after finish the job. it can wait another event.
I think the apache is working at same algorithm.

yours jansen.
xenogear 2000-03-05
  • 打赏
  • 举报
回复
这是WaitForSingleObject函数的原型
DWORD WaitForSingleObject(
HANDLE hHandle, // handle to object to wait for
DWORD dwMilliseconds // time-out interval in milliseconds
);
其中hHandle可以是等待线程的句柄, dwMilliseconds是timeout的设置
程序中可以得到该函数的返回值, 根据返回值就可以执行下一步的操作了
具体的例子可以看msdn中的例子
hehemouse 2000-03-05
  • 打赏
  • 举报
回复
怎么用,能详细点吗
xenogear 2000-03-05
  • 打赏
  • 举报
回复
可以用WaitForSingleObject

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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