c: thread in windows

发布时间 2023-10-24 22:16:50作者: ®Geovin Du Dream Park™

 

/*****************************************************************//**
 * \file   helloworld.C
 * \brief  业务操作方法
 * VSCODE   c11         安装插件“Doxygen Documentation Generator”,用来生成注释。
                        安装插件”C/C++ Snippets”,用来生成文件头、代码块分割线等。KoroFileHeader
                        C/C++ Snippets插件设置
 * \author geovindu,Geovin Du  
 * 站在巨人的肩膀上 Standing on the Shoulders of Giants
 * https://learn.microsoft.com/zh-cn/windows/win32/api/processthreadsapi/nf-processthreadsapi-createthread
 * \date   2023-09-19
***********************************************************************/ 
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include <math.h>
#include<threadpoolapiset.h>
#include<time.h>
#include<unistd.h>
#include<Windows.h>
#include <process.h>
#include "include/SortAlgorithm.h"
#include "include/CheckTieck.h"
#include "include/TakeNumber.h"
#include "include/validator.h"
#include "include/validatorView.h"


#define thread_count 5                          // Number of task threads

/* 线程共享内存 */
volatile int i = 0;

/* 线程句柄 */
HANDLE h1, h2;

typedef unsigned __int64  uintptr_t;


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
	);

DWORD WINAPI ThreadFunc(LPVOID p)
{	
	printf("我是子线程, pid = %d\n", GetCurrentThreadId());   //输出子线程pid
	return 0;
}
/**
 * @brief  启动线程
 * 
 * @param security 
 * @param stack_size 
 * @param start_address 
 * @param argilist 
 * @param initflag 
 * @param threaddr 
 * @return uintptr_t 
 */
uintptr_t beginthreadex(void* security,
	unsigned stack_size,
	unsigned(__stdcall* start_address)(void*),
	void* argilist,
	unsigned initflag,
	unsigned* threaddr);

/**
 * @brief 
 * 
 */
void endthread(void);

/**
 * @brief 
 * 
 * @param retval 
 */
void endthreadex(unsigned retval);

DWORD WaitForSingleObject(HANDLE hHandle, DWORD dwMilliseconds);

DWORD WaitForMultipleObjects(DWORD nCount,
	const HANDLE* lpHandles,
	BOOL bWaitAll,
	DWORD dwMilliseconds
);

/**
 * @brief 
 * 
 * @param arg 
 * @return unsigned 
 */
unsigned int __stdcall ThreadOne(void* arg)
{
	while (1)
	{

		i++;
        printf("One i = %d\n", i);
		Sleep(100);
         if(i==thread_count)
        {
            //endthread();
            break;
        }
	}
}

/**
 * @brief 
 * 
 * @param arg 
 * @return unsigned 
 */
unsigned int __stdcall ThreadTwo(void* arg)
{
	while (1)
	{

        i++;
		printf("Two i = %d\n", i);
		Sleep(100);
        if(i==thread_count)
        {
           // endthread();
            break;
        }

	}
}





int main(void)
{
    printf("hello c world, \n");
    printf("你好,中国\n");

    HANDLE hThread;
	DWORD  threadId;

	hThread = CreateThread(NULL, 0,	ThreadFunc, 0, 0, &threadId); // 创建线程
	printf("我是主线程, pid = %d\n", GetCurrentThreadId());  //输出主线程pid
	Sleep(2000);


    h1 = (HANDLE)_beginthreadex(NULL, 0, ThreadOne, NULL, 0, NULL);
	h2 = (HANDLE)_beginthreadex(NULL, 0, ThreadTwo, NULL, 0, NULL);
    system("pause");  //liunx 不支持
    return 0;

)

  

输出: