windows多线程 - 初级

creating threads in VC++ is simple and fast.

first of all, you need a HANDLE, you will use this HANDLE to "handle" the thread,
just like the way you "handle" a file, a socket, a device, etc.
 
If you don't care about the thread_id, you can use NULL instead.
At last, the thread function must be this prototype:
return value DWORD, one parameter, type: LPVOID
if the function is in a class, the function need to be a static one, however, there is a workaround, talk about it later.
an example thread function
 
this way, you have created a thread, and the thread is executing the function_name function with the argument you provided
in the function "function_argument". the thread_id will be the thread id of the created thread.
OK, what if you want to use a member function inside a class?
if you want to execute a member function, you can do this, it is a little tricker, but in fact, it is very easy to use, don't be scared by these at first,
 
these are called template, it is a very power tool in C++, however, you don't need to worry about these at all for now, all you need to do is, copy and paste these fuzzy code to somewhere in your code, but donot paste them into a class :)
I never tried these...in face, this template was stealed from a bbs, thx him for the sharing, it's very helpful
and, when you want to use a member function to create a thread, do this:
Let's say the class name is MyClass, and the object is myObj, if the call is inside another member function of MyClass,
you can also use "this" as the object reference.
 
of course, the a_function also need to be match the thread function prototype, such as
 
This is almost everything about threading in Windows.
(OK. I'm lying...there ARE much MORE:D, and do threading right is very HARD.)
Go threading, but don't go too far for now. 你懂的
cheer up.

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值