#include <stdio.h>
#include <windows.h>
// 线程函数
DWORD WINAPI Thread(LPVOID lpParam)
{
int number = (int)lpParam;
//获取当前线程id
DWORD threadId = GetCurrentThreadId();
while (1) {
printf("thread id is %d, number is %d\n", threadId, number--);
Sleep(1000);
}
return 0;
}
int main()
{
DWORD ThreadID1 = 0;
DWORD ThreadID2 = 0;
//创建线程
HANDLE handle1 = CreateThread(NULL, 0, Thread, (LPVOID)(1024), 0, &ThreadID1);
HANDLE handle2 = CreateThread(NULL, 0, Thread, (LPVOID)(1024), 0, &ThreadID2);
printf("main function thread1 id is %d\n", ThreadID1);
printf("main function thread2 id is %d\n", ThreadID2);
//永久等待线程运行结束
WaitForSingleObject(handle1, INFINITE);
WaitForSingleObject(handle2, INFINITE);
return 0;
}
运行结果
main function thread1 id is 25072
main function thread2 id is 9260
thread id is 9260, number is 1024
thread id is 25072, number is 1024
thread id is 9260, number is 1023
thread id is 25072, number is 1023
thread id is 25072, number is 1022
thread id is 9260, number is 1022
thread id is 25072, number is 1021
thread id is 9260, number is 1021
thread id is 9260, number is 1020
thread id is 25072, number is 1020
...
#include <stdio.h>
#include <windows.h>
#include <process.h>
unsigned int __stdcall ThreadFun(PVOID lpParam)
{
int number = (int)lpParam;
//获取当前线程id
DWORD threadId = GetCurrentThreadId();
while (1) {
printf("thread id is %d, number is %d\n", threadId, number--);
Sleep(1000);
}
return 0;
}
int main()
{
unsigned ThreadID1 = 0;
unsigned ThreadID2 = 0;
//创建线程
HANDLE handle1 = (HANDLE)_beginthreadex(NULL, 0, ThreadFun, (void*)1024, 0, &ThreadID1);
HANDLE handle2 = (HANDLE)_beginthreadex(NULL, 0, ThreadFun, (void*)1024, 0, &ThreadID2);
printf("main function thread1 id is %d\n", ThreadID1);
printf("main function thread2 id is %d\n", ThreadID2);
//永久等待线程运行结束
WaitForSingleObject(handle1, INFINITE);
WaitForSingleObject(handle2, INFINITE);
return 0;
}
运行结果
main function thread1 id is 27340
main function thread2 id is 30424
thread id is 30424, number is 1024
thread id is 27340, number is 1024
thread id is 30424, number is 1023
thread id is 27340, number is 1023
thread id is 27340, number is 1022
thread id is 30424, number is 1022
thread id is 27340, number is 1021
thread id is 30424, number is 1021
thread id is 27340, number is 1020
thread id is 30424, number is 1020
thread id is 27340, number is 1019
thread id is 30424, number is 1019
thread id is 30424, number is 1018
thread id is 27340, number is 1018
...
BOOL fFailure = (system(NOTEPAD.exe README.txt) == -1)
if(fFailure){
switch(errno){
case E2BIG: //Argument list or environment too big
break;
case ENOENT://Command interpreter cannot be found
break;
case ENOEXEC://Command interpreter has bad format
break;
case ENOMEM://Insufficient memory to run command
break;
}
}
unitptr_t __cdecl _beginthreadex(void* psa, unsigned cbStackSize, unsigned (__stdcall* pfnStartAddr)(void*), void *pvParam, unsigned dwCreateFlag, unsigned* pdwThreadID){
_ptiddata ptd; //Pointer to thread`s data block
uintptr_t thd1; //Thread`s handle
//Allocate data block for the new thread
if((ptd = (_ptiddata)_calloc_crt(1, sizeof(struct _tiddata))) == NULL){
goto error_return;
}
// Initialize the data block
initptd(ptd);
//Save the desired thread function and the parameter
//we want it to get in the data block
ptd->_initaddr = (void*)pfnStartAddr;
ptd->_initarg = pvParam;
ptd->_thandle = (uintptr_t)(-1);
//Create the new thread
thd1 = (uintptr_t)CreateThread((LPSECURITY_ATTRIBUTES)psa, cbStackSize, _threadstartex, (PVOID)ptd, dwCreateFlag, pdwThreadID);
if(thd1 == 0){
//Thread couldn`t be created, cleanup and return failure
goto error_return;
}
// Thread create OK, return the handle as unsigned long
return(thd1);
error_return:
_free_crt(ptd);
return((uintptr_t)0L);
}