多线程的问题

minoriz 2006-03-16 10:28:20
最近做一个试验,因为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是随机的,最好能解释一下其他各个变量用法
...全文
155 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
herman~~ 2006-03-16
  • 打赏
  • 举报
回复
参数通过SecondThreadFunc的那个void* pArguments参数传进去啊
传进去后,再做强制类型转换
晨星 2006-03-16
  • 打赏
  • 举报
回复
(1)参数就通过SecondThreadFunc的那个void* pArguments参数传进去啊。
(2)最后一个就是线程ID,你说有随机是什么意思啊?这是个返回参数,不是输入参数啊。

_beginthreadex的各个参数可以简介如下:
uintptr_t _beginthreadex(
void *security,
unsigned stack_size,
unsigned ( __stdcall *start_address )( void * ),
void *arglist,
unsigned initflag,
unsigned *thrdaddr
);
security
一个指向SECURITY_ATTRIBUTES结构体的指针,主要用来指定函数返回的thread handle可否被子进程继承。一般使用NULL就可以,表示不能被继承。

stack_size
新线程的栈大小。一般也用0就可以了,也是导致默认值(1M?记不清了)被使用。

start_address
新线程执行的入口地址。也就是一个函数指针,指向一个返回值和参数类型等正如你的SecondThreadFunc那样的函数。

arglist
传给线程的参数,线程创建后,它被传给了你线程函数的那个pArguments参数,这时你只要再转换一下,恢复出它原来的类型就可以保用了。

initflag
初始状态,0表示立即开始,CREATE_SUSPENDED则表示先创建好,不急着跑,后面通过调用Windows系统的API:ResumeThread来手工启动线程。

thrdaddr
你传一个变量的地址进去,函数返回后,这个变量带出来的值就是新建线程的ID。
51365133 2006-03-16
  • 打赏
  • 举报
回复
RECVPARAM *pRecvParam=new RECVPARAM;
pRecvParam->sock=m_socket;
pRecvParam->hwnd=m_hWnd;
HANDLE hThread=CreateThread(NULL,0,RecvProc,(LPVOID)pRecvParam,0,NULL);
CloseHandle(hThread);

调用处的参数处理
51365133 2006-03-16
  • 打赏
  • 举报
回复
DWORD WINAPI CChatDlg::RecvProc(LPVOID lpParameter)
{
SOCKET sock=((RECVPARAM*)lpParameter)->sock;
HWND hwnd=((RECVPARAM*)lpParameter)->hwnd;
delete lpParameter;
.
.
.
.

}

参数的传递

51365133 2006-03-16
  • 打赏
  • 举报
回复
#include <windows.h>
#include <iostream.h>

DWORD WINAPI Fun1Proc(
LPVOID lpParameter // thread data
);

DWORD WINAPI Fun2Proc(
LPVOID lpParameter // thread data
);
int index=0;
int tickets=100;
HANDLE hMutex;
void main()
{
HANDLE hThread1;
HANDLE hThread2;
hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
hThread2=CreateThread(NULL,0,Fun2Proc,NULL,0,NULL);
CloseHandle(hThread1);
CloseHandle(hThread2);
/*while(index++<1000)
cout<<"main thread is running"<<endl;*/
//hMutex=CreateMutex(NULL,TRUE,NULL);
hMutex=CreateMutex(NULL,TRUE,"tickets");
if(hMutex)
{
if(ERROR_ALREADY_EXISTS==GetLastError())
{
cout<<"only instance can run!"<<endl;
return;
}
}
WaitForSingleObject(hMutex,INFINITE);
ReleaseMutex(hMutex);
ReleaseMutex(hMutex);
Sleep(4000);
// Sleep(10);
}

DWORD WINAPI Fun1Proc(
LPVOID lpParameter // thread data
)
{
/*while(index++<1000)
cout<<"thread1 is running"<<endl;*/

/*while(TRUE)
{
//ReleaseMutex(hMutex);
WaitForSingleObject(hMutex,INFINITE);
if(tickets>0)
{
Sleep(1);
cout<<"thread1 sell ticket : "<<tickets--<<endl;
}
else
break;
ReleaseMutex(hMutex);
}*/

WaitForSingleObject(hMutex,INFINITE);
cout<<"thread1 is running"<<endl;
return 0;
}

DWORD WINAPI Fun2Proc(
LPVOID lpParameter // thread data
)
{

/*while(TRUE)
{
//ReleaseMutex(hMutex);
WaitForSingleObject(hMutex,INFINITE);
if(tickets>0)
{
Sleep(1);
cout<<"thread2 sell ticket : "<<tickets--<<endl;
}
else
break;
ReleaseMutex(hMutex);
}*/
WaitForSingleObject(hMutex,INFINITE);
cout<<"thread2 is running"<<endl;
return 0;
}

24,856

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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