您的位置:首页 > 其它

线程间之共享内存空间和线程局部存储

2012-04-22 15:22 162 查看
// test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "test.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;
int b=3;
using namespace std;
DWORD WINAPI FunProc(LPVOID lpParamer)
{
	int *a=(int *)lpParamer;
	cout<<"new thread is running"<<endl;
	cout<<b<<endl;
	cout<<*a<<endl;
	return 0;
}

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    int a=0;
	cout<<"main thread is running"<<endl;
	HANDLE hThread=CreateThread(NULL,0,FunProc,&a,0,NULL);
	Sleep(10);
	cout<<b<<endl;
	return 0;
}


各线程的主线程关闭了,则子线程会被强制关闭。这段代码注意a,b两个变量就可以了。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: