C++:创建线程初试
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 ;
}
#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++:创建线程初试的更多相关文章
- 0036 Java学习笔记-多线程-创建线程的三种方式
创建线程 创建线程的三种方式: 继承java.lang.Thread 实现java.lang.Runnable接口 实现java.util.concurrent.Callable接口 所有的线程对象都 ...
- [笔记]linux下和windows下的 创建线程函数
linux下和windows下的 创建线程函数 #ifdef __GNUC__ //Linux #include <pthread.h> #define CreateThreadEx(ti ...
- Java学习笔记-多线程-创建线程的方式
创建线程 创建线程的方式: 继承java.lang.Thread 实现java.lang.Runnable接口 所有的线程对象都是Thead及其子类的实例 每个线程完成一定的任务,其实就是一段顺序执行 ...
- python:threading多线程模块-创建线程
创建线程的两种方法: 1,直接调用threading.Thread来构造thread对象,Thread的参数如下: class threading.Thread(group=None, target= ...
- 【java并发】传统线程技术中创建线程的两种方式
传统的线程技术中有两种创建线程的方式:一是继承Thread类,并重写run()方法:二是实现Runnable接口,覆盖接口中的run()方法,并把Runnable接口的实现扔给Thread.这两种方式 ...
- Java并发编程:如何创建线程?
Java并发编程:如何创建线程? 在前面一篇文章中已经讲述了在进程和线程的由来,今天就来讲一下在Java中如何创建线程,让线程去执行一个子任务.下面先讲述一下Java中的应用程序和进程相关的概念知识, ...
- 驱动开发之 创建线程函数PsCreateSystemThread
PsCreateSystemThread 创建一个执行在内核模式的系统线程. 注意:创建线程必须用函数PsTerminateSystemThread强制线程结束.否则该线程是无法自动退出的. 函数原型 ...
- iOS开发多线程篇—创建线程
iOS开发多线程篇—创建线程 一.创建和启动线程简单说明 一个NSThread对象就代表一条线程 创建.启动线程 (1) NSThread *thread = [[NSThread alloc] in ...
- 创建线程方式-NSThread
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
随机推荐
- mysql锁机制之乐观锁(二)
select for update: 在执行这个 select 查询语句的时候,会将对应的索引访问条目进行上排他锁(X 锁),也就是说这个语句对应的锁就相当于update带来的效果. select * ...
- spring mvc 全局处理异常
spring框架支持很多种全局处理异常的方式 一.Spring MVC处理异常有4种方式: (1)使用Spring-MVC提供的SimpleMappingExceptionResolver: (2)实 ...
- PL/SQL编程—存储过程
SQL> create or replace procedure sp_pro3(name_in varchar2,id_in varchar2) is begin update mytest ...
- Druid学习之路 (三)Druid的数据源和段
作者:Syn良子 出处:https://www.cnblogs.com/cssdongl/p/9703204.html 转载请注明出处 Druid的数据源和分段 Druid的数据存储在"Da ...
- PHP 日期格式中 Y与y
大写Y效果: $nowtime = time(); $nowtime=date("Y-m-d",$nowtime); 结果: 2015-10-24 小写y效果: $nowtime ...
- Helloworld.JaVa 第一次编程
package Jc; class Jc1st { public static void main(String[] args) { System.out.println("Hello Wo ...
- 20145211《网络渗透》msf辅助模块的应用
20145211<网络渗透>msf辅助模块的应用 一.实验准备 启用VB的kali,需要用到桥接,VMware桥接总是罢工…… 二.实验步骤 最好开桥接模式,要不然你就多开几个虚拟机(只要 ...
- LeetCode (262):Nim Game
You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...
- bat(续六)-windows批处理set命令
windows批处理set命令 [设置变量]格式:set 变量名=变量值详细:被设定的变量以%变量名%引用 [取消变量]格式:set 变量名=详细:取消后的变量若被引用%变量名%将为空 [展示变量]格 ...
- cmd中如何查看环境变量
查看所有环境变量 set 查看某一个环境变量 C:\WINDOWS\system32>set no_proxyNO_PROXY=localhost,127.0.0.1,172.31.212.14 ...