1.使用CreatThread创建

#include <iostream>
#include <Windows.h>
using namespace std;
/*
创建一个线程
*/
DWORD WINAPI fun(LPVOID ipParamter)
{
while (true)
{
cout << "fun1 display!" << endl; Sleep();
}
}
/*
创建第二个线程
*/
DWORD WINAPI fun2(LPVOID i)
{
while (true)
{
cout << "fun2 " << endl; Sleep();
}
}
int main()
{
//第二个参数0是初始的字节,第五个参数0是便是立即开始线程
HANDLE hThread = CreateThread(NULL, , fun, NULL, , NULL);
HANDLE hThread2 = CreateThread(NULL, , fun2, NULL, , NULL);
CloseHandle(hThread);
CloseHandle(hThread2); while (true)
{
cout << "main display!" << endl; Sleep();
} return ;
} 
  
使用CreatThread创建线程是不安全的,容易造成内存泄漏(CRT相关),所以推荐使用_beginThread或_beginThreadex,它内部也使用了CreatThread,但是在使用前做了很多工作以确保其安全性。
 
2.使用_beginThread创建

#include <iostream>
#include <Windows.h>
#include <process.h>
using namespace std;
void fun1()
{
while (true)
{
cout << "fun1 display\n"; Sleep();
}
}
unsigned _stdcall fun2(void * )
{
while (true)
{
cout << "fun2 display\n"; Sleep();
}
}
int main()
{
unsigned int thID1, thID2;
HANDLE hfun1, hfun2;
hfun1 = (HANDLE)_beginthread((void(*)(void*))fun1, , NULL);
hfun2 = (HANDLE)_beginthreadex(NULL, , fun2, NULL, , &thID2);
WaitForSingleObject(hfun1, INFINITE); //一定要等子线程完毕
WaitForSingleObject(hfun2, INFINITE);
CloseHandle(hfun1);
CloseHandle(hfun2); cout << "end\n";
return ;
}

3.std::thread

C++11以来支持了thread类,方便了多线程的创建管理。

#include <mutex>
#include <iostream>
#include <thread>
#include <windows.h>
using namespace std;
mutex m;
void f()
{
cout << "using..." << endl;
}
int a;
void fun1(int i)
{ while (true)
{
m.lock();
//a++; cout << "f1"<< endl; Sleep(1000);
f();
Sleep();
m.unlock();
}
}
void fun2()
{ while (true)
{
m.lock();
// a--; cout << "f2"<< endl; Sleep(1000);
f();
Sleep();
m.unlock();
}
}
int main()
{
a = ;
thread th(fun1, );
thread ti(fun2);
th.join();
ti.join(); return ;
}

相关博客:C++:线程(std::thread)

  

C++:创建线程初试的更多相关文章

  1. 0036 Java学习笔记-多线程-创建线程的三种方式

    创建线程 创建线程的三种方式: 继承java.lang.Thread 实现java.lang.Runnable接口 实现java.util.concurrent.Callable接口 所有的线程对象都 ...

  2. [笔记]linux下和windows下的 创建线程函数

    linux下和windows下的 创建线程函数 #ifdef __GNUC__ //Linux #include <pthread.h> #define CreateThreadEx(ti ...

  3. Java学习笔记-多线程-创建线程的方式

    创建线程 创建线程的方式: 继承java.lang.Thread 实现java.lang.Runnable接口 所有的线程对象都是Thead及其子类的实例 每个线程完成一定的任务,其实就是一段顺序执行 ...

  4. python:threading多线程模块-创建线程

    创建线程的两种方法: 1,直接调用threading.Thread来构造thread对象,Thread的参数如下: class threading.Thread(group=None, target= ...

  5. 【java并发】传统线程技术中创建线程的两种方式

    传统的线程技术中有两种创建线程的方式:一是继承Thread类,并重写run()方法:二是实现Runnable接口,覆盖接口中的run()方法,并把Runnable接口的实现扔给Thread.这两种方式 ...

  6. Java并发编程:如何创建线程?

    Java并发编程:如何创建线程? 在前面一篇文章中已经讲述了在进程和线程的由来,今天就来讲一下在Java中如何创建线程,让线程去执行一个子任务.下面先讲述一下Java中的应用程序和进程相关的概念知识, ...

  7. 驱动开发之 创建线程函数PsCreateSystemThread

    PsCreateSystemThread 创建一个执行在内核模式的系统线程. 注意:创建线程必须用函数PsTerminateSystemThread强制线程结束.否则该线程是无法自动退出的. 函数原型 ...

  8. iOS开发多线程篇—创建线程

    iOS开发多线程篇—创建线程 一.创建和启动线程简单说明 一个NSThread对象就代表一条线程 创建.启动线程 (1) NSThread *thread = [[NSThread alloc] in ...

  9. 创建线程方式-NSThread

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

随机推荐

  1. Docker+.Net Core 的那些事儿-4.还有这种操作!?

    1.通过docker run -v命令映射工作目录 通过一系列上述操作,我们可以发现我们的发布是基于镜像的,也就是说,在后期的迭代过程中,如果有些代码修改,我们就不得不删除旧的容器和镜像,dotnet ...

  2. nginx添加sticky cookie 分流模块

    需要下载nginx源码和sticky,在nginx配置文件中添加sticky模块,然后重新编译nginx. #准备安装基础环境:yum install gcc openssl-devel pcre-d ...

  3. 20145316《Java程序设计》实验二报告

    20145316<Java程序设计>实验二报告 一.实验目的与要求 1.初步掌握单元测试和TDD. 2.理解并掌握面向对象三要素:封装.继承.多态. 3.初步掌握UML建模. 4.熟悉S. ...

  4. 【Error】安装程序无法打开注册表项 UNKNOWN\Components\...

    在安装程序的时候出现错误信息: 解决方法: 依次点击开始,所有程序,附件,右键单击命令提示符,选择以管理员身份运行.运行secedit /configure /cfg %windir%\inf\def ...

  5. SQL学习笔记之B+树

    0x00 概述 要描述清楚B+树,得先了解二叉查找数,平衡二叉树. 0x01 二叉查找树 任意节点,它的左子树如果不为空,那么左子树上所有节点的值都小于根节点的值:任意节点,他的右子树如果不为空,那么 ...

  6. mysql搜索不区分大小写

    mysql搜索是不区分大小写的,这种情况下我们有两种方法解决 知识前提: BINARY binary不是函数,而是一个类型转换运算符,它用来强制字符串为一个二进制字符串,可以理解为在字符串比较的时候区 ...

  7. STL学习笔记(不定期更新)

    algorithm *1.sort() 用法:sort(数组名,名+长度(,cmp)); int cmp(T a,T b)//T是要排序的a,b的类型,也可以是结构体中任意成员变量 { return ...

  8. SVN一直提示需要clean up

    无论到那一级都提示clean up, 这是陷入clean up 死循环的结果. 解决办法: 使用任何一款可以连sqllit 的数据库管理软件例如(Navicat Premium),连入 项目跟目录/. ...

  9. Oracle查询一个表的数据插入到另一个表

    1. 新增一个表,通过另一个表的结构和数据 create table XTHAME.tab1 as select * from DSKNOW.COMBDVERSION 2. 如果表存在: insert ...

  10. An Example for Javascript Function Scoping and Closure

    1. An Real World Example In the patron detail page of the CRM system I'm working with, there’re larg ...