c: thread in windows

 

 

/*****************************************************************//**
 * \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;

)

  

/**
 * @brief  定义一个函数,接受一个整数参数,打印该参数的平方,并返回该参数加一
 * 
 * @param x 
 * @return int 
 */
int square_and_add_one(int x) {
    printf("The square of %d is %d\n", x, x * x);
    return x + 1;
}

/**
 * @brief 定义一个线程函数,接受一个指向整数的指针,调用square_and_add_one函数,并将返回值存入指针指向的位置
 * 
 * @param arg 
 * @return DWORD 
 */
DWORD WINAPI threadFunc(LPVOID arg) {
    int *result = (int *)arg;
    *result = square_and_add_one(*result);
    return 0;
}



int main(void)
{
    printf("hello c world, \n");
    printf("你好,中国\n");
    int argc;
    char *argv;//[]={'\0'};
    char ddds[11]="The hello world";
    argv=ddds;
    argc=2;
   // 检查命令行参数的个数,至少需要一个参数
    if (argc < 2) {
        fprintf(stderr, "Usage: %s <number>\n", argv[0]);
        exit(1);
    }
   printf("%s",argv[0]);
    // 将第一个参数转换为整数
    int numthread =0;//atoi(argv[0]);

    // 创建一个线程句柄
    HANDLE hThread;

    // 创建一个线程,并传入thread_func函数和num的地址
    hThread = CreateThread(NULL, 0, threadFunc, &numthread, 0, NULL);

    // 检查线程是否创建成功
    if (hThread == NULL) {
        fprintf(stderr, "CreateThread failed: %d\n", GetLastError());
        exit(2);
    }

    // 等待线程结束,并关闭线程句柄
    WaitForSingleObject(hThread, INFINITE);
    CloseHandle(hThread);

    // 打印线程返回的值
    printf("The thread returned %d\n", num);

    system("pause");  //liunx 不支持
    return 0;
     
}

  

输出:

 

posted @ 2023-10-24 22:14  ®Geovin Du Dream Park™  阅读(3)  评论(0编辑  收藏  举报