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. ionic 打包

    比如我下载一个官方的小Demoionic start myApp tabs(我默认你已经配置好了安卓的开发环境.安装了全局的ionic.cordova)   首先,你需要给项目添加安卓平台的支持ion ...

  2. sgu 100 A+B 解题报告及测试数据

    100.A+B time limit per test: 0.25 sec. memory limit per test: 65536 KB 题解:上手题,不解释. 直接上代码: #include & ...

  3. [Python]返回函数,装饰器拾遗

    def lazy_print(*args): def pr(): print(args) return pr 当我们调用lazy_print()时,返回的并不是求和结果,而是求和函数: >> ...

  4. (译)Windows Azure:移动后端开发的主要更新

    Windows Azure:移动后端开发的主要更新 这周我们给Windows Azure释出了一些很棒的更新,让云上的移动应用开发明显的简单了.这 些新功能包括: 移动服务:定制API支持移动服务:G ...

  5. Ubuntu&Linux系统出现文件系统只读Read-only file system 的快速解决方法

    问题描述: 周末运行盘平台服务程序,周一来操作系统卡顿,主进程已退出,重启进程时提示Read-only file system:新建目录和其他chmod -R等等操作都提示Read-only file ...

  6. python_threading模块实现多线程详解(转)

    综述 Python这门解释性语言也有专门的线程模型,Python虚拟机使用GIL(Global Interpreter Lock,全局解释器锁)来互斥线程对共享资源的访问,但暂时无法利用多处理器的优势 ...

  7. 清除list或者map集合,减少内存的占用率

    1.在编写程序对数据处理的时候我们经常会使用到list和map集合,有些时候我们存到集合里的数据只是暂时性的,在验证完或者使用完之后尽量对list或者map清空,及list.clear()后者map. ...

  8. 运行bat时隐藏cmd窗口

    运行bat时隐藏cmd窗口 新建一个shrjj.vbs文件,文件内容为: Set ws = CreateObject("Wscript.Shell") ws.run "c ...

  9. 20145313张雪纯 《Java程序设计》第3周学习总结

    20145313张雪纯 <Java程序设计>第3周学习总结 教材学习内容总结 4.1.1 定义类 package four; class Clothes{ String color; ch ...

  10. uboot dm9000驱动故障

    手头有一块6410开发板,已经有别人提供的uboot代码(基于2011.06),但是在检测dm9000时显示下面的输出: Net: No ethernet found. 当然其他网络命令例如ping等 ...