thread demo

// testCPP.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <stdio.h>
#include <tchar.h>
#include <time.h>
#include <string>
#include <iostream>
//#include  <fstream>
#include <windows.h>
#define DESC "test thread"   //aaaa
using namespace std;


//void  openFile(std::ostream *vsFile)
//{
// if(*vsFile)
// {
// *vsFile<<"this is just for test \r\n"<<std::endl;
// }
// else
// {
// cout<<"open file error!"<<endl;
// }
//}

HANDLE m_mutex;
DWORD WINAPI ThreadProc1(LPVOID lpParameter);
DWORD WINAPI ThreadProc2(LPVOID lpParameter);
int _tmain(int argc, _TCHAR* argv[])
{
int is=2;
long j=3;
is=(int)j;
HANDLE thread1=CreateThread(NULL,0,ThreadProc1,NULL,0,NULL);
HANDLE thread2=CreateThread(NULL,0,ThreadProc2,NULL,0,NULL);
// CloseHandle(thread1);
// CloseHandle(thread2);
string straa="";
straa=DESC;
int i=strlen(DESC);
cout<<i<<"---lenght\r\n"<<endl;
cout<<straa<<endl;
m_mutex=CreateMutex(NULL,FALSE,NULL);
getchar();
return 0;
}

DWORD WINAPI ThreadProc1(LPVOID lpParameter)
{
while(1)
{
  WaitForSingleObject(m_mutex,INFINITE);
  cout<<"thread 1 run\r\n"<<endl;
  ReleaseMutex(m_mutex);
  Sleep(800);
//   break;
}
 return 0;
}
DWORD WINAPI ThreadProc2(LPVOID lpParameter)
{
while(1)
{
  WaitForSingleObject(m_mutex,INFINITE);
  cout<<"thread 2 run\r\n"<<endl;
  ReleaseMutex(m_mutex);
  Sleep(800);
//   break;
}
 return 0;
}


//根据分区,有十个线程来处理这个文件的生成

原文地址:https://www.cnblogs.com/xianqingzh/p/1602378.html