线程数目是动态的

解决方案 »

  1.   

    //Get all threads
    std::deque<DWORD> dqThreads;
    HANDLE hThreadSnap = NULL; 
    BOOL bRet = FALSE; 
    THREAD_INFORMATION_EX tie;
    DWORD dwThisThread = ::GetCurrentThreadId(); // used for not killing ourself // Take a snapshot of all threads currently in the system. 
    DWORD dwProcessID=GetCurrentProcessId();
    hThreadSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, dwProcessID); 
    if (hThreadSnap == INVALID_HANDLE_VALUE) 
    return false; 
    // Fill in the size of the structure before using it.
    tie.te32.dwSize = sizeof(THREADENTRY32); 
    // Walk the thread snapshot to find all threads of the process. 
    // If the thread belongs to the process, add its information 
    // to the display list.
    if (Thread32First(hThreadSnap, &tie.te32)) 

    do 

    if (tie.te32.th32OwnerProcessID == dwProcessID) 

    dqThreads.push_back(tie.te32.th32ThreadID);
    ZeroMemory(&tie, sizeof(THREAD_INFORMATION_EX));
    tie.te32.dwSize = sizeof(THREADENTRY32);


    while (Thread32Next(hThreadSnap, &tie.te32)); 
    bRet = TRUE; 

    //else 
    // return false;          // could not walk the list of threads 
    CloseHandle (hThreadSnap); 
    //
    HANDLE ThreadArray[64];
    ...
    DWORD dwRet=WaitForMultipleObjects(ThreadCount,ThreadArray, TRUE, 5000);
      

  2.   

    semaphore
    WINDOWS高级编程里面有一个地方专门讨论了这个
      

  3.   

    //Get  all  threads  
    std::deque  <DWORD  >  dqThreads;  
               HANDLE                                                            hThreadSnap  =  NULL;    
               BOOL                                                            bRet  =  FALSE;    
               THREAD_INFORMATION_EX            tie;  
               DWORD                                                            dwThisThread  =  ::GetCurrentThreadId();            //  used  for  not  killing  ourself  
     
               //  Take  a  snapshot  of  all  threads  currently  in  the  system.    
               DWORD  dwProcessID=GetCurrentProcessId();  
               hThreadSnap  =  CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD,  dwProcessID);    
               if  (hThreadSnap  ==  INVALID_HANDLE_VALUE)    
                           return  false;                
               //  Fill  in  the  size  of  the  structure  before  using  it.              
               tie.te32.dwSize  =  sizeof(THREADENTRY32);                
               //  Walk  the  thread  snapshot  to  find  all  threads  of  the  process.    
               //  If  the  thread  belongs  to  the  process,  add  its  information    
               //  to  the  display  list.              
               if  (Thread32First(hThreadSnap,  &tie.te32))    
               {    
                           do    
                           {    
                                       if  (tie.te32.th32OwnerProcessID  ==  dwProcessID)    
                                       {    
                                                   dqThreads.push_back(tie.te32.th32ThreadID);  
                                                   ZeroMemory(&tie,  sizeof(THREAD_INFORMATION_EX));  
                                                   tie.te32.dwSize  =  sizeof(THREADENTRY32);  
                                       }    
                           }    
                           while  (Thread32Next(hThreadSnap,  &tie.te32));    
                           bRet  =  TRUE;    
               }    
               //else    
               //            return  false;                    //  could  not  walk  the  list  of  threads    
               CloseHandle  (hThreadSnap);    
               //  
               HANDLE  ThreadArray[64];  
    ...  
    DWORD  dwRet=WaitForMultipleObjects(ThreadCount,ThreadArray,  TRUE,  5000);
      

  4.   

    我向线程发送自定义消息,线程接收到消息后会正常退出。但是,如果我在发完消息后,使用
    WaitForSingleObject(hThread,INFINITE)等待线程退出,
    有时候会一直等待下去。
    这是为什么呢。请指教。
      

  5.   

    使用WaitForMultipleObjects()函数.
      

  6.   

    masterz(MS MVP) 的方法有一定的实用性。收藏
      

  7.   

    这个涉及到线程同步,多线程需要使用同步对象和等待函数来实现同步。
    WIN32 API提供了一组能使线程阻塞其自身执行的等待函数,这些函数只有在作为其参数的一个或多个同步对象产生信号时才返回,在超过规定的等待时间后,不管有无信号,函数都会返回,在等待函数未返回时,线程处于等待状态,此时线程只消耗很少的CPU时间,使用等待函数即可以保证线程同步,又可以提高效率,常用的是:
    DWORD WaitForSingleObject,同时监测多个同步对象函数:WaitForMultipleObjects共有三种同步对象:事件、mutex和信号灯(semaphore,也就是你们上面所说的)事件对象是最简单的同步对象,该对象是用CreateEvent函数建立的,SetEvent可以把事件对象设置为有信号状态  ResetEvent把事件对象设置为无信号状态mutex对象的状态在它不被任何线程拥有时是有信号的,而当它被拥有时则是无信号的,该对象适合用来协调多线程对共享资源的互斥访问该线程用CreateMutex来建立,ReleaseMutex释放,如果线程终止而不释放,则认为被废弃信号灯对象维护一个从0开始的计数,线程用CreateSemaphore函数来建立信号灯对象,别的进程可以用OpenSemaphore函数打开指定名字的信号灯句柄,一般把信号灯初始计数设置为最大值,每当信号灯有信号使等待函数返回时,信号灯计数就会减1,而调用ReleaseSemaphore可以增加信号灯的计数,数值越小表明访问共享资源的程序越多。
      

  8.   

    在主进程用一个变量m_bQuit(初始化为FALSE),线程的循环中对该变量检查,若为TRUE,线程自己退出。然后在主进程要求终止所有线程的地方把该变量赋值为TRUE,接着调用WaitForMultipleObjects(pThreads,2000),再检查个线程的运行状态以终止和结束它们
      

  9.   

    awant2k 说的后边儿还对,前边儿就不行了,用变量可以解决还用操作系统的同步机制做什么啊?
      

  10.   

    真高兴啊!有见到高手了。
    WaitForMultipleObject()函数等待所有的线程返回!
      

  11.   

    masterz的方法有个缺陷,得到的线程列表是处于get snapshot 时的列表,如果在此之后还有线程被创建,有可能会有遗漏(很微小的可能)。可以通过一些标志变量解决。awant2k的方法则需要主线程来维护线程句柄列表,程序上会麻烦点。
    另外就是WaitForMultipleObject()有64个的限制,我觉得可以使用event+ interlock的那2个函数解决。
    线程退出时interlockdec... 发送 event,主线程收到event后读取计数。
      

  12.   

    只要线程数目不超过64个,调用WaitForMultiObject是完全可行的。。