前几天简单对C和C++中的创建多线程的函数进行了测试,这篇随笔就简单介绍一下创建线程的相关函数。
C中三个创建线程函数:pthread_create()、_beginthread()、CreateThread()
三个关闭线程函数:pthread_exit()、_endthread()、ExitThread()
头文件分别是:pthread.h、process.h、processthreadsapi.h
pthread_create()函数原型:
int pthread_create(pthread_t *tidp,
const pthread_attr_t *attr,
(void*)(*start_rtn)(void*),
void *arg);
_beginthread()函数原型:
uintptr_t _beginthread(void( *start_address )( void * ),
unsignedstack_size,void *arglist);
_beginthread()函数是调用CreateThread()函数来实现的,但多了一些功能,是C标准库中提供的函数。而CreateThread()函数创建一个线程在调用进程的虚拟地址空间内执行,是操作系统中的函数。可参考解释:http://bbs.csdn.net/topics/21693。
CreateThread()函数原型:
HANDLE WINAPI CreateThread(
_In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes,
_In_ SIZE_T dwStackSize,
_In_ LPTHREAD_START_ROUTINE lpStartAddress,
_In_opt_ LPVOID lpParameter,
_In_ DWORD dwCreationFlags,
_Out_opt_ LPDWORD lpThreadId
);
pthread_exit()函数原型:
void pthread_exit(void *retval);
_endthread()函数原型:
void _endthread( void );
void _endthreadex(
unsigned retval
);
ExitThread()函数原型:
VOID WINAPI ExitThread(
_In_ DWORD dwExitCode
);
貌似要使用pthread_exit();需要安全释放,否则会出现内存泄露:pthread_exit()
下面测试一下各个函数:
pthread.h:
#include
#include
#include
#define MAXTHREADS 5
void* Run(void* args)
{
printf("%d:Hello World!\n", *((int*)args));
pthread_exit(NULL);
return NULL;
}
int main()
{
pthread_t tid[MAXTHREADS];
pthread_attr_t attr;
void*status;
int ret;
pthread_attr_init(&attr);//初始化attr
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);//设置获得attr的线程为可连接的:PTHREAD_CREATE_JOINABLE 原型为 0
for(int i = 0; i < MAXTHREADS; i++)
{
if((ret = pthread_create(&tid[i], &attr, Run, (void*)&i)) != 0)//获得attr,则线程可连接
{
puts("Pthread_Create error!");
exit(1);
}
pthread_join(tid[i], &status);//等待线程结束,结束的线程便不能再被连接
}
pthread_attr_destroy(&attr);//销毁attr资源
return 0;
}
pthread_join等待其他线程结束后再连接能连接的函数,已经结束的线程便不能再被连接。所以输出结果可看到是按顺序执行的。感觉就是加上了互斥锁,不知道理解对不对?
process.h:
#include
#include
#include
#define MAXTHREAD 5
void run(void*i)
{
printf("%d:Hello world!\n", *((int*)i));
}
int main()
{
for(int i = 0; i < MAXTHREAD; i++) {
_beginthread(&run, 0, (void*)&i);
}
_endthread();
return 0;
}
消除了类型强转的警告,但程序无法正常退出,有时也无法正常进行。
--------------------update 2018-02-24 16:09:01---------------------
processthreadsapi.h:
#include
#define MAXTHREAD 5
HANDLE ghMutex;
DWORD WINAPI Run_Process(LPVOID p) {
WaitForSingleObject(ghMutex, INFINITE);
try
{
TCHAR s = *((int*)p)+'0';
MessageBoxA(NULL, TEXT("Hello world!"), TEXT(&s), MB_ICONWARNING | MB_OK);
ReleaseMutex(ghMutex);
}
catch(...)
{
if (! ReleaseMutex(ghMutex))
MessageBoxA(NULL, TEXT("ReleaseMutex Error!"), TEXT("ERROR"), MB_ICONERROR | MB_OK);
}
return GetLastError();
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE HPrevInstance, PSTR szCmdLine, int iCmdShow)
{
DWORD dwThreadIdArray[MAXTHREAD];
HANDLE hThreadArray[MAXTHREAD];
if((ghMutex = CreateMutex(NULL, FALSE, NULL)) == NULL)
{
ExitProcess(1);
}
for(int i = 0; i < MAXTHREAD; i++) {
hThreadArray[i] = CreateThread(
NULL,
0,
Run_Process,
(LPVOID)&i,
0,
&dwThreadIdArray[i]);
if (hThreadArray[i] == NULL)
{
ExitProcess(3);
}
}
WaitForMultipleObjects(MAXTHREAD, hThreadArray, TRUE, INFINITE);
for(int i = 0; i < MAXTHREAD; i++)
{
CloseHandle(hThreadArray[i]);
}
CloseHandle(ghMutex);
return 0;
}
参考我的这篇随笔:
http://www.cnblogs.com/darkchii/p/8447863.html
-------------------------------
参考资料:
http://www.man7.org/linux/man-pages/man3/pthread_exit.3.html
http://www.man7.org/linux/man-pages/man3/pthread_create.3.html
http://www.cppblog.com/prayer/archive/2012/04/23/172427.html
https://msdn.microsoft.com/en-us/library/windows/desktop/ms682659(v=vs.85).aspx
https://msdn.microsoft.com/en-us/library/windows/desktop/ms682453(v=vs.85).aspx
https://msdn.microsoft.com/zh-cn/library/hw264s73.aspx