C入门:简单的win32线程

来源:互联网 发布:网络英语课程哪个好 编辑:程序博客网 时间:2024/06/05 01:56
// 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 filevoid 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;}


原创粉丝点击