Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2570744
  • 博文数量: 315
  • 博客积分: 3901
  • 博客等级: 少校
  • 技术积分: 3640
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-08 15:32
个人简介

知乎:https://www.zhihu.com/people/monkey.d.luffy Android高级开发交流群2: 752871516

文章分类

全部博文(315)

文章存档

2019年(2)

2018年(1)

2016年(7)

2015年(32)

2014年(39)

2013年(109)

2012年(81)

2011年(44)

分类: C/C++

2013-09-07 18:50:14

        不管是linux也好,windows也好,学习就好!之前也用boost库尝试做了个线程池啥的,没做完,各种思考,毕竟真正接触的少!各种看资料,各种学习。过程中扎实好语言的基础,同时最重要的是,看着手册啥的来,比较能够学到东西,就是这样!
      周末时间在看那个linux多线程服务端编程,说实话,比较不简单!只能是看点是点..还是太嫩了

点击(此处)折叠或打开

  1. // Thread.cpp : Defines the entry point for the console application.
  2. //

  3. #include "stdafx.h"
  4. #include <windows.h>

  5. DWORD WINAPI ThreadProc(LPVOID lpParameter);

  6. int main(int argc, char* argv[])
  7. {
  8.     int i = 0;
  9.     DWORD test = 0;    ///< 0表示第一个子线程
  10.     HANDLE handle;
  11.     DWORD numThreadId = 0;
  12.     
  13.     for (i = 0; i < 6; ++i)
  14.     {
  15.         ///< 线程创建完毕后立即执行【由第5个参数决定】
  16.         handle = CreateThread(NULL, 0, ThreadProc, (LPVOID)&test, 0, &numThreadId);
  17.         if (handle)
  18.         {
  19.             Sleep(1000);        ///< 为了让test每次是不同的值
  20.             test = numThreadId;    ///< 表示"上一个"子线程线程ID
  21.             printf("thread %lld success start...\n", numThreadId);
  22.             CloseHandle(handle);
  23.         }
  24.     }
  25.     Sleep(1000);
  26.     
  27.     return 0;
  28. }


  29. DWORD WINAPI ThreadProc(LPVOID lpParameter)
  30. {
  31.     if (0 == *((DWORD *)lpParameter))
  32.     {
  33.         printf("currunt thread is run\n");
  34.     }
  35.     else
  36.     {
  37.         printf("forward threadID is %lld\n", *((DWORD *)lpParameter));
  38.     }
  39.     
  40.     return 0;
  41. }

  42. /************************************************************************/
  43. /* 运行结果
  44. currunt thread is run
  45. thread 2680 success start...
  46. forward threadID is 2680
  47. thread 3432 success start...
  48. forward threadID is 3432
  49. thread 3928 success start...
  50. forward threadID is 3928
  51. thread 1176 success start...
  52. forward threadID is 1176
  53. thread 2520 success start...
  54. forward threadID is 2520
  55. thread 3216 success start...
  56. Press any key to continue */
  57. /************************************************************************/

阅读(14495) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~