C语言多线程编程一

C语言多线程编程一

c语言中有一个函数可以实现简单的多线程编程,它的函数原型为:

   uintptr_t _beginthread( 
   void( *start_address )( void * ),
   unsigned stack_size,
   void *arglist 
);

Parameters 参数:
start_address:程序执行一个新线程的起始地址,即你写的执行函数名
Start address of a routine that begins execution of a new thread. For _beginthread, the calling convention is either __cdecl or __clrcall; for _beginthreadex, it is either __stdcall or __clrcall.

stack_size:新线程的堆栈大小或0,为0表示和主线程共用堆栈
Stack size for a new thread or 0.

Arglist:传给新线程的变量清单或空
Argument list to be passed to a new thread or NULL.

Return Value 返回值:
如果新线程建立成功,函数返回该线程的句柄;然而,如果新线程退出太快,_beginthread函数可能返回一个有误的句柄。_beginthread发生错误时返回1L。

例如:
开发环境是:win10+vs2013


#define _CRT_SECURE_NO_WARNINGS  //关闭安全检查
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <process.h>   //使用多线程必须要加上的头文件

void thread_1(void *p){  //参数类型必须是void *p类型
	
		MessageBoxA(NULL, "hello", "hello", 0);
}
int main(void){
	for (int i = 0; i < 10; i++){
	HANDLE handle1 = _beginthread(thread_1, 0, NULL);
    }
	//WaitForSingleObject(handle1, INFINITE);  //加上这条语句就变成同步,否则就是异步。
	system("pause");
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值