#include "stdafx.h"
#include
#include
#include //获得时间
#include //va_start
#include //用 _mkdir()创建一个文件夹(目录)
#include //access
#include //MultiByteToWideChar
#pragma comment(lib, "shlwapi.lib")
#include
#include
//#include //多线程相关操作头文件,可移植众多平台linux下
#define FILE_NAME "D:\\Server1\\Serverserver2\\serverServer4\\server.log"
#define NUM_THREADS 5 //线程数
///////////////////////////////////////////////////////////////////////
//基本功能性模块定义
///////////////////////////////////////////////////////////////////////
//共享锁
#if defined(_WIN32)
class MyLock
{
CRITICAL_SECTION m_Lock ;
public :
MyLock( ){ InitializeCriticalSection(&m_Lock); } ;
~MyLock( ){ DeleteCriticalSection(&m_Lock); } ;
VOID Lock( ){ EnterCriticalSection(&m_Lock); } ;
VOID Unlock( ){ LeaveCriticalSection(&m_Lock); } ;
};
#elif defined(__LINUX__)
class MyLock
{
pthread_mutex_t m_Mutex;
public :
MyLock( ){ pthread_mutex_init( &m_Mutex , NULL );} ;
~MyLock( ){ pthread_mutex_destroy( &m_Mutex) ; } ;
VOID Lock( ){ pthread_mutex_lock(&m_Mutex); } ;
VOID Unlock( ){ pthread_mutex_unlock(&m_Mutex); } ;
};
#endif
MyLock g_log_lock ;
static int count = 0;
unsigned int __stdcall WriteLog(void *pM)
{
char *filename = FILE_NAME;
char *msg = "\r\n(###) main..." ;
g_log_lock.Lock();
switch (count)
{
case 0:
Sleep(6000);
break;
case 1:
Sleep(1000);
break;
case 2:
Sleep(10000);
break;
case 3:
Sleep(8000);
break;
case 4:
Sleep(3000);
break;
}
char info[2048];
memset(info, 0, sizeof(info));
va_list argptr;
va_start(argptr, msg);
vsprintf(info,msg,argptr);
va_end(argptr);
time_t timep;
struct tm *p;
time(&timep);
char now[30];
//p = gmtime(&timep);
p = localtime(&timep);
strftime(now,64,"%Y-%m-%d %H:%M:%S",p);
strcat( info, now ) ;
WCHAR wszClassName[256];
memset(wszClassName,0,sizeof(wszClassName));
MultiByteToWideChar(CP_ACP,0,filename,strlen(filename)+1,wszClassName,
sizeof(wszClassName)/sizeof(wszClassName[0]));
FILE* fp = fopen( filename, "ab+" ) ;
//if是多余的,因为上一个函数已经做过处理。可以直接写文件
if (!fp)
{
//创建文件
if(_mkdir(filename)==0)
{
FILE* fp = fopen( filename, "ab" ) ;
fwrite(info, strlen(info), 1, fp ) ;
fclose(fp) ;
}
}
else
{
fwrite(info, strlen(info), 1, fp ) ;
fclose(fp) ;
}
printf("线程ID号为%4d的子线程报数%d\n", GetCurrentThreadId(),count++);
g_log_lock.Unlock();
return 0;
}
void WriteFile(char *fileName)
{
char *tag;
for(tag=fileName;*tag;tag++)
{
if (*tag=='\\')
{
char buf[1000],path[1000];
strcpy(buf,fileName);
buf[strlen(fileName)-strlen(tag)+1]=NULL;
strcpy(path,buf);
if (access(path,6)==-1)
{
mkdir(path);
}
}
}
FILE* fp = fopen( fileName, "ab+" ) ;
if (!fp)
{
//创建文件
if(_mkdir(fileName)==0);
}
}
void crete_thred()
{
HANDLE handle[NUM_THREADS];
for (int i=0;i
//经典线程同步互斥问题
#include "stdafx.h"
#include
#include
#include
long g_nNum;
unsigned int __stdcall Fun(void *pPM);
const int THREAD_NUM = 10;
//信号量与关键段
HANDLE g_hThreadParameter;
CRITICAL_SECTION g_csThreadCode;
int _tmain(int argc, _TCHAR* argv[])
{
printf(" 经典线程同步 信号量Semaphore\n");
//printf(" -- by MoreWindows( http://blog.csdn.net/MoreWindows ) --\n\n");
//初始化信号量和关键段
g_hThreadParameter = CreateSemaphore(NULL, 0, 1, NULL);//当前0个资源,最大允许1个同时访问
InitializeCriticalSection(&g_csThreadCode);
HANDLE handle[THREAD_NUM];
g_nNum = 0;
int i = 0;
while (i < THREAD_NUM)
{
handle[i] = (HANDLE)_beginthreadex(NULL, 0, Fun, &i, 0, NULL);
WaitForSingleObject(g_hThreadParameter, INFINITE);//等待信号量>0
++i;
}
WaitForMultipleObjects(THREAD_NUM, handle, TRUE, INFINITE);
//销毁信号量和关键段
DeleteCriticalSection(&g_csThreadCode);
CloseHandle(g_hThreadParameter);
for (i = 0; i < THREAD_NUM; i++)
CloseHandle(handle[i]);
return 0;
}
unsigned int __stdcall Fun(void *pPM)
{
int nThreadNum = *(int *)pPM;
ReleaseSemaphore(g_hThreadParameter, 1, NULL);//信号量++
Sleep(50);//some work should to do
EnterCriticalSection(&g_csThreadCode);
++g_nNum;
Sleep(0);//some work should to do
printf("线程编号为%d 全局资源值为%d\n", nThreadNum, g_nNum);
LeaveCriticalSection(&g_csThreadCode);
getchar();
return 0;
}
#include "stdafx.h"
#include
#include
#include
//子线程函数
unsigned int __stdcall ThreadFun(PVOID pM)
{
int count = 0;
while(GetCurrentThreadId()%20==0)
{
printf("111线程ID号为%4d的子线程\n", GetCurrentThreadId());
Sleep(2000);
count++;
if (count==5)
{
Sleep(10000);
break;
}
}
if (count==0)
{
printf("222线程ID号为%4d的子线程\n", GetCurrentThreadId());
}
return 0;
}