c: thread in windows
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | /*****************************************************************/ /** * \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; ) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | /** * @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; } |
输出:
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
Cpp programming
标签:
线程
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2022-10-24 Python: Observer Pattern
2022-10-24 Python: Mediator Pattern
2011-10-24 Csharp windowform bindingNavigator,bindingSource,DataGridView簡單分頁:首頁,上一頁,下一頁,末頁
2009-10-24 World Currency Symbols世界货币符号