// windows_35_Thread_Tls.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <windows.h>#include <stdio.h>#include <string.h>int g_AddVar = 0;CHAR *g_pszText1 = NULL;__declspec(thread)CHAR *g_pszText2 = NULL;void print( ){//printf( "Text1:%s\n", g_pszText1 );printf( "Text2:%s\n", g_pszText2 );}DWORD WINAPI PrintProc( LPVOID pParam ){CHAR *pszText = (CHAR*)pParam;g_pszText1 = (CHAR *)malloc( 100 );memset( g_pszText1, 0, 100 );strcpy_s( g_pszText1, strlen(pszText)+1, pszText );g_pszText2 = (CHAR *)malloc( 100 );memset( g_pszText2, 0, 100 );//strcpy_s( g_pszText2, strlen( pszText ) + 1, pszText );sprintf_s( g_pszText2, 100, "%s,%d", pszText, ++g_AddVar );while (true){print( );Sleep( 1000 );}}void Create( ){//1、定义线程处理函数//ThreadProc//2、创建线程//CreateThreadDWORD dwThread = 0;CHAR szText1[] = "Thread 1------------";HANDLE hThread = CreateThread( NULL, 0, PrintProc, szText1, 0, &dwThread );//线程2CHAR szText2[] = "Thread 2*************";hThread = CreateThread( NULL, 0, PrintProc, szText2, 0, &dwThread );//线程3CHAR szText3[] = "Thread 3xxxxxxxxxxxxxxxx";hThread = CreateThread( NULL, 0, PrintProc, szText3, 0, &dwThread );//3、使用线程//4、结束线程//ExitThread//TerminateThread//5、线程挂起和执行//挂起线程、SuspendThread//执行线程、ResumeThread//6、等候线程的结束//WaitForSingleObject//7、关闭线程句柄//CloseHandlegetchar( );}int _tmain(int argc, _TCHAR* argv[]){Create( );return 0;}
35 windows_35_Thread_Tls 线程局部存储
原文:http://www.cnblogs.com/nfking/p/5573572.html