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

来源:互联网 发布:阿里云降配置 编辑:程序博客网 时间:2024/05/17 23:22
// test.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "test.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// The one and only application objectCWinApp 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两个变量就可以了。

原创粉丝点击