VC++ MSDN中的 _beginthreadex与_endthreadex 的使用例子

1._beginthread, _beginthreadex .
用于创建线程
[cpp] view plaincopy
  1. uintptr_t _beginthread(   
  2.    void( *start_address )( void * ),  
  3.    unsigned stack_size,  
  4.    void *arglist   
  5. );  
  6. uintptr_t _beginthreadex( //推荐使用  
  7.    void *security, //安全属性,NULL为默认安全属性  
  8.    unsigned stack_size, //指定线程堆栈的大小。如果为0,则线程堆栈大小和创建它的线程的相同。一般用0  
  9.    unsigned ( *start_address )( void * ), //指定线程函数的地址,也就是线程调用执行的函数地址(用函数名称即可,函数名称就表示地址)  
  10.    void *arglist, //传给线程的参数指针;传多个参数时请用结构体 //可以通过传入对象的指针,在线程函数中再转化为对应类的指针  
  11.    unsigned initflag, //线程初始状态,0:立即运行;CREATE_SUSPEND:suspended(悬挂)  
  12.    unsigned *thrdaddr //用于记录线程ID的地址  
  13. );  


参数:

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.
对于_beginthread调用约定 是__cdecl__clrcall; 对于_beginthreadex,则either__stdcall__clrcall

stack_size

Stack size for a new thread or 0.

arglist

Argument list to be passed to a new thread or NULL.

security

Pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes. If NULL, the handle cannot be inherited. Must be NULL for Windows 95 applications.

initflag

Initial state of a new thread (0 for running or CREATE_SUSPENDED for suspended); use ResumeThread to execute the thread.

thrdaddr

Points to a 32-bit variable that receives the thread identifier. Might be NULL, in which case it is not used.


2._endthread, _endthreadex
用于关闭各个使用_beginthread or _beginthreadex创建的线程
[cpp] view plaincopy
  1. void _endthread( void );  
  2. void _endthreadex( //推荐使用  
  3.    unsigned retval   
  4. );  


例子:

[cpp] view plaincopy
  1. // crt_begthrdex.cpp  
  2. // compile with: /MT  
  3. #include <windows.h>  
  4. #include <stdio.h>  
  5. #include <process.h>  
  6.   
  7.   
  8. unsigned Counter;   
  9. unsigned __stdcall SecondThreadFunc( void* pArguments )  
  10. {  
  11.     printf( "In second thread...\n" );  
  12.   
  13.   
  14.     while ( Counter < 1000000 )  
  15.         Counter++;  
  16.   
  17.   
  18.     _endthreadex( 0 );  
  19.     return 0;  
  20. }   
  21.   
  22.   
  23. int main()  
  24. {   
  25.     HANDLE hThread;  
  26.     unsigned threadID;  
  27.   
  28.   
  29.     printf( "Creating second thread...\n" );  
  30.   
  31.   
  32.     // Create the second thread.  
  33.     hThread = (HANDLE)_beginthreadex( NULL, 0, &SecondThreadFunc, NULL, 0, &threadID );  
  34.   
  35.   
  36.     // Wait until second thread terminates. If you comment out the line  
  37.     // below, Counter will not be correct because the thread has not  
  38.     // terminated, and Counter most likely has not been incremented to  
  39.     // 1000000 yet.  
  40.     WaitForSingleObject( hThread, INFINITE );  
  41.     printf( "Counter should be 1000000; it is-> %d\n", Counter );  
  42.     // Destroy the thread object.  
  43.     CloseHandle( hThread );  
  44. }  


输出:

[cpp] view plaincopy
  1. Creating second thread...  
  2. In second thread...  
  3. Counter should be 1000000; it is-> 1000000  


一些比较具体的例子:

http://hi.baidu.com/qinpc/blog/item/5aaa8b54918e541b3a2935ee.html

http://www.cppblog.com/mzty/archive/2007/07/25/28756.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/351421.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

「澳洋主数据项目」主数据促企业变革

part 1 企业简介&#xff1a; 澳洋集团是一家跨地区、多元化的民营企业集团&#xff0c;总部位于全国百强县市前三甲的江苏省张家港市。集团成立于1998年7月&#xff0c;2007年经国家工商总局核准&#xff0c;升格为免冠行政区划的大型集团企业。集团现有37家下属子&#xff08…

linux bin目录误删,Linux下误删 /user/bin目录后的补救

当危险的动作发生&#xff0c; 误删 /user/bin目录后的补救以下是昨天晚上真实的误操作现场&#xff0c;模拟记录一下(这是测试环境&#xff0c;所以操作得很随意&#xff0c;有些执行动作很不规范)在上面编译一个软件Dboop&#xff0c;完事以后想把它做个软链到 /usr/binsudo …

使用JFlex生成词法分析器 1:安装配置

环境&#xff1a;Windows 10 STEP 1&#xff1a; 下载 JFlex 文件&#xff0c;我选择的是 jflex-1.7.0.zip。下载完成后解压到想安装的位置。 文件结构如下&#xff08;假设解压目录为 C:\&#xff09;&#xff1a; C:\jflex-1.7.0\ --bin\ (start scri…

问题: 将N个元素使用push_back插入到vector中, 求push_back操作的复杂度。

简单分析如下&#xff1a; 考虑vector每次内存扩充两倍的情况。 如果我们插入N个元素&#xff0c; 则会引发lgN次的内存扩充&#xff0c;而每次扩充引起的元素拷贝次数为 2^0, 2^1, 2^2, ..., 2^lgN. 把所有的拷贝次数相加得到 2^0 2^1 2^2 ... 2^lgN 2 * 2^lgN - 1 约为…

linux superblock 时间,Linux命令(八)

1、文件系统:windows的FAT&#xff0c;win2000以后的NTFS文件系统&#xff0c;Linux的正规文件系统为EXT2(Linux second extended file system&#xff0c;Ext2fs)传统方式中&#xff0c;一个分区只能格式化为一个分区。由于新技术的利用&#xff0c;一个分区可以格式化为多个文…

在Amazon Elastic Beanstalk上部署Spring Boot应用程序

在此博客中&#xff0c;我们将看到如何在Amazon ElasticBeanstalk上部署Spring Boot应用程序。 Amazon ElasticBeanstalk具有一个预配置的Java环境&#xff0c;可用于部署内部装有servlet容器的Spring Boot JAR。 对于我们的示例&#xff0c;此处将使用maven作为构建工具。 …

Linux上静态库和动态库的编译和使用

linux上静态库和动态库的编译和使用&#xff08;附外部符号错误浅谈&#xff09; 这就是静态库和动态库的显著区别&#xff0c;静态库是编译期间由链接器通过include目录找到并链接到到可执行文件中&#xff0c;而动态库则是运行期间动态调用&#xff0c;只有运行时找不到对应动…

Linux Socket API Connect 函数详解

在讲解套接字编程函数之前&#xff0c;有必要对socket编程的两个不可或缺的结构体进行说明。 第一个结构体式struct sockaddr.。这个结构为许多类型的套接字储存套接字地址信息&#xff1a; Sockaddr结构体介绍 #include<sys/socket.h> struct sockaddr { …

java 适用参数_Java功能的适用性

java 适用参数Java语言和标准库功能强大&#xff0c;但功能强大&#xff0c; 责任重大 。 一方面看到很多用户代码滥用或滥用稀有的Java功能&#xff0c;另一方面却完全忘记了大多数基本功能之后&#xff0c;我决定撰写此摘要。 这不是每个Java开发人员都应该探索&#xff0c;了…

linux查看tar进程进度,Linux:wget后台下载/查看后台任务进度

今天在自己的服务器上使用wget下载一个大文件时&#xff0c;不小心把ssh断开连接了&#xff0c;重新登上去后想查看这个文件的下载进度&#xff0c;现记录一些wget的知识点。1&#xff1a;后台下载使用wget -b url[root8f9fbda9bb48 ~]# wget -b http://cn.wordpress.org/word…

【redis】在windos下的redis服务器的搭建

1.下载Redis-x64-3.2.100&#xff08;楼主用的版本&#xff0c;需要安装包的可以找我要&#xff09; 下载官方版本 2.解压后在cmd下运行 redis-server redis.windos.conf 此时redis服务已经在该windows下6379端口运行 3.把该服务设置成windos服务 redis-server --service-insta…

《Linux网络接口》---------struct ifreq struct ifconf

网络接口--------------struct ifconf&#xff0c;struct ifreq 网络相关的ioctl请求的request参数及arg地址必须指向的数据类型如下表所示&#xff1a; 接口 SIOCGIFCONF SIOCSIFADDR SIOCGIFADDR SIOCSIFBRDADDR SIOCGIFBRDADDR SIOCSIFNETMASK SIOCGIFNETMASK 获取所有接口…

会议季Mic Drop:您不应该错过的13场Java演讲

您的老板没有派您参加真正的会议吗&#xff1f; 我们为您准备了最好的讲座 9月主要发生在一些重大事件上&#xff1a;秋季的第一天&#xff0c;甚至全国熏肉日。 这也是召开会议最忙的月份之一&#xff0c;一些大型Java事件涵盖了平台的新的重要更新。 在下面的帖子中&#x…

linux7设备的挂载,centos7磁盘分区与挂载解析

Linux系统在磁盘、U盘以及光盘等设备分区和挂载操做才能使用。centos1、磁盘分区原理与规则ui磁盘分区类型&#xff1a;主分区&#xff0c;扩展分区&#xff0c;逻辑分区this分区规则&#xff1a;centos7一、主分区扩展分区的数量不能超过4个&#xff0c;且扩展分区只能有1个。…

【公众号系列】SAP的新零售

公众号&#xff1a;SAP Technical本文作者&#xff1a;matinal原文出处&#xff1a;http://www.cnblogs.com/SAPmatinal/ 原文链接&#xff1a;【公众号系列】SAP的新零售写在前面 还是以前的一篇文章&#xff08;一八年三月&#xff09;&#xff0c;拿出来重新了解一下。 随着…

linux下汇编语言开发总结

汇编语言是直接对应系统指令集的低级语言&#xff0c;在语言越来越抽象的今天&#xff0c;汇编语言并不像高级语言那样使用广泛&#xff0c;仅仅在驱动程序&#xff0c;嵌入式系统等对性能要求苛刻的领域才能见到它们的身影。但是这并不表示汇编语言就已经没有用武之地了&#…

使用openocd调试Linux内核,openocd安装与调试

环境&#xff1a;硬件&#xff1a;PC机ARM仿真器v8.00已下载好bit流的Xinlinx SoC开发板(其上有arm cortex-a9核)软件&#xff1a;Redhat Linux6(或虚拟机) openocd使用openocd下载程序&#xff0c;调试arm cortex-a9核。一、openocd安装下载libusb库安装或直接yum install li…

execl中设置的格式无法实现

在一次项目中&#xff0c;需要导出execl表&#xff0c;并且要给表中的表格设置格式&#xff0c;因为每列的格式都不一样&#xff0c;需要单独设置设置这些格式&#xff0c;在后期使用中因为导入的数据过多&#xff0c;是的后面的单元格中设置的格式无法实现。 每次打开execl表格…

loadrunner监控linux性能指标,使用LoadRunner监控Linux系统性能.doc

使用LoadRunner监控Linux系统性能性能监控案例■秘密 □机密 □绝密PAGELinux系统性能监控案例(仅供内部使用)版 本 号&#xff1a;V0.1保 密 等 级&#xff1a;■秘密 □机密 □绝密编 制&#xff1a;XXX审 核&#xff1a;修订记录日期版本号描述作者2011-06-130.1初稿完成目录…

github gists_Eclipse中的Github Gists

github gists我想描述有关在Eclipse中集成GitHub Gists的简单步骤。 有几个来源促使我这样做&#xff1a; Eclipse的GitHub Mylyn连接器 EGit / GitHub /用户指南 http://eclipse.github.com 我一直在使用Eclipse Java EE发行版&#xff0c;其中已经安装了Mylyn插件&#…