C入门:简单的win32线程

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#pragma once


#define WIN32_LEAN_AND_MEAN		// Exclude rarely-used stuff from Windows headers
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#include <windows.h>
#include <process.h>

void t1();

typedef struct 
{
	int vi;
	char vs[20];
}Args,ThreadArgs;

/** 参数复杂点的线程测试 */
VOID thread3(PVOID pvoid);
void threadT3();

/** 带简单参数的线程测试 */
VOID thread2(PVOID pvoid);
void threadT2();

/** 不带参数的线程测试 */
VOID thread1(PVOID pvoid);
void threadT1();

// TODO: reference additional headers your program requires here


// stdafx.cpp : source file that includes just the standard includes
// thread_test1.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information

#include "stdafx.h"

// TODO: reference any additional headers you need in STDAFX.H
// and not in this file

void t1()
{
	Args* a = (Args*) malloc(sizeof(Args) );
	
	free(a);
}

void threadT3()
{
	ThreadArgs* args = (ThreadArgs*) malloc( sizeof(ThreadArgs));

	args->vi = 2090;
	strcpy(args->vs, "Max");

	_beginthread(thread3, 0, args);

	
}

VOID thread3(PVOID pvoid)
{
	ThreadArgs* args = (ThreadArgs*) pvoid;

	printf("vi:%d,vs:%s\n", args->vi, args->vs);

	free(args);
}

void threadT2()
{
	int i = 100;
	_beginthread (thread2, 0, &i) ;

	for(int i = 0; i < 50; i++)
	{
		Sleep(100);
	}
}

VOID thread2(PVOID pvoid)
{
	//printf("%d\n", *((int*)pvoid));

	for(int i = *((int*)pvoid),end = i+20; i < end; i++)
	{		
		printf("number: %d\n", i);
		Sleep(100);		
	}	
}

void threadT1()
{
	_beginthread (thread1, 0, NULL) ;

	for(int i = 0; i < 10; i++)
	{
		Sleep(100);
	}
}

VOID thread1(PVOID pvoid)
{
	for(int i = 0; i < 20; i++)
	{		
		Sleep(100);
		printf("number: %d\n", i);
	}	
}



// thread_test1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
	//t1();
	threadT3();
	//threadT2();
	//threadT1();	

	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值