windows下,C语言创建线程

1、使用CreateThread()、ExitThread(0)等系统调用创建和撤销线程

  1. #include "stdafx.h"  
  2. #include "线程控制.h"  
  3.   
  4. #ifdef _DEBUG  
  5. #define new DEBUG_NEW  
  6. #undef THIS_FILE  
  7. static char THIS_FILE[] = __FILE__;  
  8. #endif  
  9.   
  10. /  
  11. // The one and only application object  
  12.   
  13. CWinApp theApp;  
  14.   
  15. using namespace std;  
  16.   
  17. void T1();  
  18.   
  19. static HANDLE hHandle1 = NULL;  
  20. DWORD dw1;  
  21.   
  22. int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])  
  23. {  
  24.     int nRetCode = 0;  
  25.   
  26.     hHandle1 = CreateThread((LPSECURITY_ATTRIBUTES) NULL,  
  27.                             0,  
  28.                             (LPTHREAD_START_ROUTINE) T1,  
  29.                             (LPVOID) NULL,  
  30.                             0,  
  31.                             &dw1);  
  32.     printf("32222\n");  
  33.     printf("32222\n");  
  34.     printf("32222\n");  
  35.     Sleep(1000);  
  36.     CloseHandle(hHandle1);  
  37.     ExitThread(0);  
  38.   
  39.   
  40.     return nRetCode;  
  41. }  
  42. void T1(){  
  43.     printf("23333\n");  
  44.     printf("23333\n");  
  45.     printf("23333\n");  
  46. }  

2、向线程所在函数传递一个参数

  1. #include "stdafx.h"  
  2. #include "cs.h"  
  3.   
  4. #ifdef _DEBUG  
  5. #define new DEBUG_NEW  
  6. #undef THIS_FILE  
  7. static char THIS_FILE[] = __FILE__;  
  8. #endif  
  9.   
  10. /  
  11. // The one and only application object  
  12.   
  13. CWinApp theApp;  
  14.   
  15. using namespace std;  
  16.   
  17. void T1(int *x);  
  18.   
  19. static HANDLE hHandle1 = NULL;  
  20. DWORD dw1;  
  21.   
  22. int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])  
  23. {  
  24.     int nRetCode = 0;  
  25.     int *x ;  
  26.     x = (int *)malloc(sizeof(int));  
  27.       
  28.     scanf("%d",x);  
  29.     hHandle1 = CreateThread((LPSECURITY_ATTRIBUTES) NULL,  
  30.                             0,  
  31.                             (LPTHREAD_START_ROUTINE) T1,  
  32.                             (LPVOID)x,  
  33.                             0,  
  34.                             &dw1);  
  35.     printf("main:%d\n",*x);  
  36.       
  37.     Sleep(1000);  
  38.     CloseHandle(hHandle1);  
  39.     ExitThread(0);  
  40.   
  41.     return nRetCode;  
  42. }  
  43. void T1(int *x){  
  44.     printf("T1:%d",*x);  
  45. }  

3、 向线程所在函数传递多个参数
  1. #include "stdafx.h"  
  2. #include "csf.h"  
  3.   
  4. #ifdef _DEBUG  
  5. #define new DEBUG_NEW  
  6. #undef THIS_FILE  
  7. static char THIS_FILE[] = __FILE__;  
  8. #endif  
  9.   
  10. /  
  11. // The one and only application object  
  12.   
  13. CWinApp theApp;  
  14.   
  15. using namespace std;  
  16.   
  17. typedef struct{  
  18.     int x;  
  19.     int y;  
  20. }czm;  
  21.   
  22. void T1(czm *p);  
  23. static HANDLE hHandle1 = NULL;  
  24. DWORD dw;  
  25.   
  26.   
  27.   
  28. int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])  
  29. {  
  30.     int nRetCode = 0;  
  31.       
  32.     czm *p;  
  33.     p = (czm*) malloc (sizeof(czm));  
  34.     p->x = 2;  
  35.     p->y = 3;  
  36.     hHandle1 = CreateThread((LPSECURITY_ATTRIBUTES) NULL,  
  37.                             0,  
  38.                             (LPTHREAD_START_ROUTINE) T1,  
  39.                             (LPVOID) p,  
  40.                             0,  
  41.                             &dw);  
  42.       
  43.     printf("main:%d+%d=%d\n",p->x,p->y,(p->x+p->y));  
  44.     Sleep(1000);  
  45.     CloseHandle(hHandle1);  
  46.     ExitThread(0);  
  47.   
  48.   
  49.     return nRetCode;  
  50. }  
  51.   
  52. void T1(czm *p){  
  53.     printf("T1:%d*%d=%d\n",p->x,p->y,(p->x*p->y));  
  54. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值