#include <windows.h>
#include <process.h>
/* _beginthread, _endthread */
这是一个创建线程的宏,必须在
工程->设置->C/C++->Code Generation->Use run-time libray->选 Debug Multithread(多线程),或 Multithread
才可以使用,不然会出现,没有定义的错误。。。
#include <stddef.h>
#include <stdio.h>
UINT WINAPI Thread_1(LPVOID para)
{
HANDLE *handle = (HANDLE*)(para);
WaitForSingleObject(*handle, INFINITE);
printf("start_1/n");
Sleep(2000);
printf("end_1/n");
ReleaseMutex(*handle);
return 0;
}
UINT WINAPI Thread_2(LPVOID para)
{
HANDLE *handle = (HANDLE*)(para); //得到主函数中的线程的句柄
WaitForSingleObject(*handle, INFINITE); //等待对象,第二个参数表示,时间是无限的。。。
printf("start_2/n");
Sleep(2000);
printf("end_2/n");
ReleaseMutex(handle);
return 0;
}
int main()
{
HANDLE handle = CreateMutex(NULL, FALSE, NULL);
//创建两个线程,得到他们的句柄。。。
//设置了线程的完成函数,第三个参数为传入完成函数的参数,和过滤驱动中的用法一样
HANDLE handle_1 = (HANDLE)_beginthreadex(NULL, 0, Thread_1, &handle, 0, NULL);
HANDLE handle_2 = (HANDLE)_beginthreadex(NULL, 0, Thread_2, &handle, 0, NULL);
Sleep(6000);
return 0;
}
Creates a thread.
uintptr_t _beginthread( void( *start_address )( void * ), unsigned stack_size, void *arglist ); uintptr_t _beginthreadex( void *security, unsigned stack_size, unsigned ( *start_address )( void * ), void *arglist, unsigned initflag, unsigned *thrdaddr ); //创建线程函数的说明 |