//TLS线程局部存储的函数实现
//
#include "stdafx.h"
#include "windows.h"
#include "tchar.h"
#include "locale.h"
TCHAR * g_pszText=NULL;
DWORD g_nTlsIndex=0;
VOID Print()
{
//_tprintf(TEXT("g_pszText:%s\n"),g_pszText);
//从指定的TLS索引中获取值
TCHAR * pszText=(TCHAR *)TlsGetValue(g_nTlsIndex);
_tprintf(TEXT("TLS:%s\n"),pszText);
}
DWORD WINAPI PrintProc(LPVOID lpParameter)
{
TCHAR * pszText=(TCHAR *)lpParameter;
g_pszText=(TCHAR *)malloc(100);
_tcscpy(g_pszText,pszText);
TlsSetValue(g_nTlsIndex,g_pszText);
while(1)
{
Print();
Sleep(1000);
}
return 0;
}
VOID Create()
{
HANDLE hTread=NULL;
DWORD nThreadId=0;
TCHAR szText1[]=TEXT("线程1-------------");
hTread=CreateThread(NULL,0,PrintProc,szText1,0,&nThreadId);
TCHAR szText2[]=TEXT("-------------线程2");
hTread=CreateThread(NULL,0,PrintProc,szText2,0,&nThreadId);
WaitForSingleObject(hTread,INFINITE);
}
int _tmain(int argc, _TCHAR* argv[])
{
setlocale(LC_ALL,"zhi");
//创建TLS索引号
g_nTlsIndex=TlsAlloc();
Create();
//释放TSL索引号
TlsFree(g_nTlsIndex);
return 0;
}
// 本程序演示原子锁的使用方式
//
#include "stdafx.h"
#include "windows.h"
#include "tchar.h"
#include "locale.h"
LONG g_lValue1=0;
LONG g_lValue2=0;
DWORD WINAPI InterProc1(LPVOID lpParameter)
{
for (int nIndex=0;nIndex<10000000;nIndex++)
{
g_lValue1++;
}
return 0;
}
DWORD WINAPI InterProc2(LPVOID lpParameter)
{
for (int nIndex=0;nIndex<10000000;nIndex++)
{
InterlockedIncrement(&g_lValue2);
}
return 0;
}
VOID Create()
{
DWORD wThreadId=0;
HANDLE hTread[4]={NULL};
hTread[0]=CreateThread(NULL,0,InterProc1,NULL,0,&wThreadId);
hTread[1]=CreateThread(NULL,0,InterProc1,NULL,0,&wThreadId);
hTread[2]=CreateThread(NULL,0,InterProc2,NULL,0,&wThreadId);
hTread[3]=CreateThread(NULL,0,InterProc2,NULL,0,&wThreadId);
WaitForMultipleObjects(4,hTread,TRUE,INFINITE);
//线程1和线程2都执行完毕之后,打印出数值,然后退出
_tprintf(TEXT("Value1=%d,Value2=%d\n"),g_lValue1,g_lValue2);
}
int _tmain(int argc, _TCHAR* argv[])
{
Create();
return 0;
}
//本程序演示临界区的使用方式//#include "stdafx.h"#include "windows.h"#include "tchar.h"#include "locale.h"CRITICAL_SECTION cs={0};LONG lValue=0;VOID Print(){ //进入临界区,临界区中可以有多条语句,会作为一个整体执行 EnterCriticalSection(&cs); _tprintf(TEXT("LONG long long .........\n")); lValue++;//这个语句的意思就是说明临界区可以实现原子锁的功能 LeaveCriticalSection(&cs);}DWORD WINAPI PrintProc( LPVOID lpParameter ){ while(1) { Print(); Sleep(1); } return 0;}VOID Create(){ DWORD nThreadID=0; HANDLE hThread[2]={0}; hThread[0]=CreateThread(NULL,0,PrintProc,NULL,0,&nThreadID); hThread[1]=CreateThread(NULL,0,PrintProc,NULL,0,&nThreadID); getchar();}int _tmain(int argc, _TCHAR* argv[]){ InitializeCriticalSection(&cs); Create(); DeleteCriticalSection(&cs); return 0;}
//本程序演示事件的使用方式#include "stdafx.h"#include "windows.h"#include "tchar.h"#include "locale.h"HANDLE g_hEvent1=NULL;HANDLE g_hEvent2=NULL;DWORD WINAPI ThreadSend( LPVOID lpParameter){ while(1) { SetEvent(g_hEvent1);//触发事件1 Sleep(500); SetEvent(g_hEvent2);//触发事件2 Sleep(500); } return 0;}DWORD WINAPI ThreadRecv1( LPVOID lpParameter){ while(1) { WaitForSingleObject(g_hEvent1,INFINITE); _tprintf(TEXT("Hello Event :%p\n"),g_hEvent1); } return 0;}DWORD WINAPI ThreadRecv2( LPVOID lpParameter){ while(1) { WaitForSingleObject(g_hEvent2,INFINITE); _tprintf(TEXT("Hello Event :%p\n"),g_hEvent2); ResetEvent(g_hEvent2);//因为是手动重置事件,所以要手动的把事件设置成为无信号状态 } return 0;}VOID Create(){ DWORD wThreadID=0; HANDLE hTread[3]={NULL}; hTread[0]=CreateThread(NULL,0,ThreadSend,NULL,0,&wThreadID); hTread[1]=CreateThread(NULL,0,ThreadRecv1,NULL,0,&wThreadID); hTread[2]=CreateThread(NULL,0,ThreadRecv2,NULL,0,&wThreadID);}int _tmain(int argc, _TCHAR* argv[]){ //创建自动重置事件 g_hEvent1=CreateEvent(NULL,FALSE,FALSE,NULL);//创建一个自动重置事件,初始信号没有,匿名事件 //创建一个手动事件 g_hEvent2=CreateEvent(NULL,TRUE,FALSE,NULL);//创建一个手动重置事件,初始信号没有,匿名事件 Create(); getchar(); CloseHandle(g_hEvent1); CloseHandle(g_hEvent2); return 0;}
<pre name="code" class="cpp">//本程序演示互斥量的使用方式#include "stdafx.h"#include "windows.h"#include "tchar.h"#include "locale.h"HANDLE g_Mutex=NULL;DWORD WINAPI ThreadProc1( LPVOID lpParameter){ while(1) { WaitForSingleObject(g_Mutex,INFINITE); _tprintf(TEXT("线程1-------------------------\n")); Sleep(500); ReleaseMutex(g_Mutex); } return 0;}DWORD WINAPI ThreadProc2( LPVOID lpParameter){ while(1) { WaitForSingleObject(g_Mutex,INFINITE); _tprintf(TEXT("-------------线程2------------\n")); Sleep(500); ReleaseMutex(g_Mutex); } return 0;}DWORD WINAPI ThreadProc3( LPVOID lpParameter){ while(1) { WaitForSingleObject(g_Mutex,INFINITE); _tprintf(TEXT("-------------------------线程3\n")); Sleep(500); ReleaseMutex(g_Mutex); } return 0;}VOID Create(){ DWORD wThreadID=0; HANDLE hTread[3]={NULL}; hTread[0]=CreateThread(NULL,0,ThreadProc1,NULL,0,&wThreadID); hTread[1]=CreateThread(NULL,0,ThreadProc2,NULL,0,&wThreadID); hTread[2]=CreateThread(NULL,0,ThreadProc3,NULL,0,&wThreadID);}int _tmain(int argc, _TCHAR* argv[]){ setlocale(LC_ALL,"zhi"); //创建互斥量 g_Mutex=CreateMutex(NULL,FALSE,NULL); Create(); getchar(); CloseHandle(g_Mutex); return 0;}
//本程序演示信号量的使用方式#include "stdafx.h"#include "windows.h"#include "tchar.h"#include "locale.h"#include "stdio.h"#include "conio.h"HANDLE g_hSemphore=NULL;DWORD WINAPI ThreadSend( LPVOID lpParameter){ while(1) { TCHAR ch=_getch(); switch(ch) { case '1': _tprintf(TEXT("将会打印出一个语句!\n")); ReleaseSemaphore(g_hSemphore,1,NULL); break; case '5': ReleaseSemaphore(g_hSemphore,5,NULL); _tprintf(TEXT("将会打印出五个语句!\n")); break; } } return 0;}DWORD WINAPI ThreadRecv( LPVOID lpParameter){ while(1) { WaitForSingleObject(g_hSemphore,INFINITE); _tprintf(TEXT("Hello Semaphore!\n")); Sleep(1000); } return 0;}VOID Create(){ DWORD wThreadID=0; HANDLE hTread[2]={NULL}; hTread[0]=CreateThread(NULL,0,ThreadSend,NULL,0,&wThreadID); hTread[1]=CreateThread(NULL,0,ThreadRecv,NULL,0,&wThreadID); WaitForMultipleObjects(2,hTread,TRUE,INFINITE); }int _tmain(int argc, _TCHAR* argv[]){ setlocale(LC_ALL,"zhi"); //创建互斥量 g_hSemphore=CreateSemaphore(NULL,5,10,NULL);// 初始的时候5个信号量,也就是信号量创建出来的时候就会立刻发出5个信号。 Create(); getchar(); CloseHandle(g_hSemphore); return 0;}
//本程序演示可等候定时器的使用方式
#include "stdafx.h"
#include "windows.h"
#include "tchar.h"
#include "locale.h"
#include "stdio.h"
#include "conio.h"
HANDLE g_hTimer=NULL;
VOID CALLBACK TimerAPCProc(LPVOID lpArgToCompletionRoutine,DWORD dwTimerLowValue,DWORD dwTimerHighValue)
{
_tprintf(TEXT("----------------APC TIMER PROC----------\n"));
}
DWORD WINAPI TimerThread( LPVOID lpParameter)
{
while(1)
{
WaitForSingleObject(g_hTimer,INFINITE);
_tprintf(TEXT("Hello Timer!\n"));
}
return 0;
}
VOID Create()
{
//创建一个定时器
g_hTimer=CreateWaitableTimer(NULL,FALSE,NULL);
LARGE_INTEGER DueTime={0};
DueTime.QuadPart=-100000000;
//设置定时器
SetWaitableTimer(g_hTimer,&DueTime,1000,NULL,NULL,FALSE);
//创建等候线程
DWORD wThreadID=0;
HANDLE hTread=NULL;
hTread=CreateThread(NULL,0,TimerThread,NULL,0,&wThreadID);
WaitForSingleObject(hTread,INFINITE);
CloseHandle(hTread);
}
VOID APCTimer()
{
HANDLE hTimer=CreateWaitableTimer(NULL,FALSE,NULL);
LARGE_INTEGER DueTime={0};
DueTime.QuadPart=-10000;
SetWaitableTimer(hTimer,&DueTime,1000,TimerAPCProc,NULL,FALSE);
while (1)
{
SleepEx(-1,TRUE);
}
CloseHandle(hTimer);
}
int _tmain(int argc, _TCHAR* argv[])
{
setlocale(LC_ALL,"zhi");
//Create();
APCTimer();
return 0;
}