#include <windows.h>
#include <stdio.h>
#include <process.h>unsigned Counter;
unsigned __stdcall SecondThreadFunc( void* pArguments )
{
printf( "In second thread...\n" ); while ( Counter < 1000000 )
Counter++; _endthreadex( 0 );
return 0;
} int main()
{
HANDLE hThread;
unsigned threadID; printf( "Creating second thread...\n" ); // Create the second thread.
hThread = (HANDLE)_beginthreadex( NULL, 0, &SecondThreadFunc, NULL, 0, &threadID ); // Wait until second thread terminates. If you comment out the line
// below, Counter will not be correct because the thread has not
// terminated, and Counter most likely has not been incremented to
// 1000000 yet.
WaitForSingleObject( hThread, INFINITE );
printf( "Counter should be 1000000; it is-> %d\n", Counter );
// Destroy the thread object.
CloseHandle( hThread );
}
#include <stdio.h>
#include <process.h>unsigned Counter;
unsigned __stdcall SecondThreadFunc( void* pArguments )
{
printf( "In second thread...\n" ); while ( Counter < 1000000 )
Counter++; _endthreadex( 0 );
return 0;
} int main()
{
HANDLE hThread;
unsigned threadID; printf( "Creating second thread...\n" ); // Create the second thread.
hThread = (HANDLE)_beginthreadex( NULL, 0, &SecondThreadFunc, NULL, 0, &threadID ); // Wait until second thread terminates. If you comment out the line
// below, Counter will not be correct because the thread has not
// terminated, and Counter most likely has not been incremented to
// 1000000 yet.
WaitForSingleObject( hThread, INFINITE );
printf( "Counter should be 1000000; it is-> %d\n", Counter );
// Destroy the thread object.
CloseHandle( hThread );
}
解决方案 »
- 浏览器有返回值吗?
- 简单问题:Windows 2K最大使用句柄数为多少,超过多少就不安全了?
- 怎么样用一个无模式对话框中的编辑框来实时显示主程序中的变量?
- 菜鸟问题::: 请问栈和堆栈的区别!!
- 紧急求教:怎样讲一个文件的内容读入到一个字符串中去(包括\n换行符)!!!
- 只有LIB和.H文件,而没有dll文件,怎么运行总是要dll文件?在线送分。
- 为什么用VC时有时会导致诺顿杀毒navapsvc.exe非常忙?
- 请问:FTP服务器的工作原理是怎样的?
- ADO求字段名?
- :我将给这位高人108分(分三次给,何如?)!如何将MATLAB中的C函数直接用到VC中,而不需用MATLAB的编译器LCC,并且程序不需打开MATLAB集成环境?
- 哪里能下载到谭浩强的<<Visual C++ 6.0实用教程>> ?
- 高手指点~!请问如何使用复选框,在线等,50~~分~~~
#include <stdio.h>DWORD WINAPI ThreadFunc( LPVOID lpParam )
{
printf( "Parameter = %d.", *(DWORD*)lpParam ); return 0;
}
VOID main( VOID )
{
DWORD dwThreadId, dwThrdParam = 1;
HANDLE hThread; hThread = CreateThread(
NULL, // default security attributes
0, // use default stack size
ThreadFunc, // thread function
&dwThrdParam, // argument to thread function
0, // use default creation flags
&dwThreadId); // returns the thread identifier
// Check the return value for success.
if (hThread == NULL)
{
printf( "CreateThread failed (%d)\n", GetLastError() );
}
else
{
_getch();
CloseHandle( hThread );
}
}
我想在关机函数{}中调用一个线程函数,在这个线程函数中还要添加一个判断语句if()else{}
我想问的是怎么建立并启动一个线程函数,线程函数怎么调用?麻烦给讲下,先谢谢了.
我想在关机函数{}中调用一个线程函数,在这个线程函数中还要添加一个判断语句if()else{}
我想问的是怎么建立并启动一个线程函数,线程函数怎么调用?麻烦给讲下,先谢谢了.------------------------
汗