线程数监视器

编程入门 行业动态 更新时间:2024-10-22 12:25:00
本文介绍了线程数监视器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在代码中创建了3个线程:

I have created 3 threads in my code:

#include <windows.h> #include <strsafe.h> #define MAX_THREADS 3 #define BUF_SIZE 255 typedef struct _MyData { int val1; int val2; } MYDATA, *PMYDATA; DWORD WINAPI ThreadProc( LPVOID lpParam ) { HANDLE hStdout; PMYDATA pData; TCHAR msgBuf[BUF_SIZE]; size_t cchStringSize; DWORD dwChars; hStdout = GetStdHandle(STD_OUTPUT_HANDLE); if( hStdout == INVALID_HANDLE_VALUE ) return 1; // Cast the parameter to the correct data type. pData = (PMYDATA)lpParam; // Print the parameter values using thread-safe functions. StringCchPrintf(msgBuf, BUF_SIZE, TEXT("Parameters = %d, %d\n"), pData->val1, pData->val2); StringCchLength(msgBuf, BUF_SIZE, &cchStringSize); WriteConsole(hStdout, msgBuf, cchStringSize, &dwChars, NULL); // Free the memory allocated by the caller for the thread // data structure. HeapFree(GetProcessHeap(), 0, pData); return 0; } void main() { PMYDATA pData; DWORD dwThreadId[MAX_THREADS]; HANDLE hThread[MAX_THREADS]; int i; // Create MAX_THREADS worker threads. for( i=0; i<MAX_THREADS; i++ ) { // Allocate memory for thread data. pData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MYDATA)); if( pData == NULL ) ExitProcess(2); // Generate unique data for each thread. pData->val1 = i; pData->val2 = i+100; hThread[i] = CreateThread( NULL, // default security attributes 0, // use default stack size ThreadProc, // thread function pData, // argument to thread function 0, // use default creation flags &dwThreadId[i]); // returns the thread identifier // Check the return value for success. if (hThread[i] == NULL) { ExitProcess(i); } } // Wait until all threads have terminated. WaitForMultipleObjects(MAX_THREADS, hThread, TRUE, INFINITE); // Close all thread handles upon completion. for(i=0; i<MAX_THREADS; i++) { CloseHandle(hThread[i]); } }

但是在性能监视器,进程浏览器之类的工具中,它仅显示一个线程数. 甚至在创建了3个线程之后,这些工具仍只显示一个线程的原因可能是什么? 为什么我的线程数不为3?

But in tools like performance monitor, process explorer it show only one thread count. What may be the reason that even after creating 3 threads these tools are showing only one? Why I am not getting the thread count of 3?

推荐答案

3个线程没有执行任何需要花费大量时间的事情,因此可能因为它们启动和消失太快,以至于任何性能工具都无法注意到它们曾经被创建过. 我在计算机上通过在线程proc内放置Sleep(1000);进行了检查.没有它,Process Explorer不会注意到这三个线程,有了它们,它们的寿命就足以显示出来,然后迅速从线程列表中消失. The 3 threads don''t do anything that takes a significant amount of time so it''s probably because they start and die too quickly for any performance tool to notice they were ever created. I checked this on my machine by putting a Sleep(1000); inside the thread proc. Without it Process Explorer doesn''t notice the three threads, with it they live just long enough to show up and then quickly disappear from the thread list.

//为什么我没有得到线程数为3? 只需设置的通话 // Why I am not getting the thread count of 3? Just set the call of ::MessageBox(NULL, _T("Please click OK to end this thread"), _T("Done"), MB_OK)

在ThreadProc返回之前:)

before your ThreadProc returns :)

更多推荐

线程数监视器

本文发布于:2023-11-11 06:13:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1577597.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:监视器   线程

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!