多线程的问题
最近做一个试验,因为java的速度要求达不到,所以改写成C++。
因为以前C++没用过,所以对多线程不太熟悉。
以下是我在CSDN论坛中所找到的代码,希望大家解释一下:
#include <iostream>
#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 );
}
首先这段代码中SecondThreadFunc做为另一个线程启动。我有一个疑问,如果进程需要传递一些参数给后面的多个线程,这些参数该通过什么方法传递给(全局变量不能达到要求)。
另外在SecondThreadFunc中最后一个参数threadID有什么作用没有?是表示这个线程的ID?但这段代码这个threadid是随机的,最好能解释一下其他各个变量用法