码迷,mamicode.com
首页 > 编程语言 > 详细

35 windows_35_Thread_Tls 线程局部存储

时间:2016-06-10 13:43:31      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:

windows_35_Thread_Tls 线程局部存储

  1. // windows_35_Thread_Tls.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include <windows.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. int g_AddVar = 0;
  8. CHAR *g_pszText1 = NULL;
  9. __declspec(thread)CHAR *g_pszText2 = NULL;
  10. void print( )
  11. {
  12. //printf( "Text1:%s\n", g_pszText1 );
  13. printf( "Text2:%s\n", g_pszText2 );
  14. }
  15. DWORD WINAPI PrintProc( LPVOID pParam )
  16. {
  17. CHAR *pszText = (CHAR*)pParam;
  18. g_pszText1 = (CHAR *)malloc( 100 );
  19. memset( g_pszText1, 0, 100 );
  20. strcpy_s( g_pszText1, strlen(pszText)+1, pszText );
  21. g_pszText2 = (CHAR *)malloc( 100 );
  22. memset( g_pszText2, 0, 100 );
  23. //strcpy_s( g_pszText2, strlen( pszText ) + 1, pszText );
  24. sprintf_s( g_pszText2, 100, "%s,%d", pszText, ++g_AddVar );
  25. while (true)
  26. {
  27. print( );
  28. Sleep( 1000 );
  29. }
  30. }
  31. void Create( )
  32. {
  33. //1、定义线程处理函数
  34. //ThreadProc
  35. //2、创建线程
  36. //CreateThread
  37. DWORD dwThread = 0;
  38. CHAR szText1[] = "Thread 1------------";
  39. HANDLE hThread = CreateThread( NULL, 0, PrintProc, szText1, 0, &dwThread );
  40. //线程2
  41. CHAR szText2[] = "Thread 2*************";
  42. hThread = CreateThread( NULL, 0, PrintProc, szText2, 0, &dwThread );
  43. //线程3
  44. CHAR szText3[] = "Thread 3xxxxxxxxxxxxxxxx";
  45. hThread = CreateThread( NULL, 0, PrintProc, szText3, 0, &dwThread );
  46. //3、使用线程
  47. //4、结束线程
  48. //ExitThread
  49. //TerminateThread
  50. //5、线程挂起和执行
  51. //挂起线程、SuspendThread
  52. //执行线程、ResumeThread
  53. //6、等候线程的结束
  54. //WaitForSingleObject
  55. //7、关闭线程句柄
  56. //CloseHandle
  57. getchar( );
  58. }
  59. int _tmain(int argc, _TCHAR* argv[])
  60. {
  61. Create( );
  62. return 0;
  63. }





35 windows_35_Thread_Tls 线程局部存储

标签:

原文地址:http://www.cnblogs.com/nfking/p/5573572.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!