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; } ...
随机推荐
- XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem J. Terminal
题目:Problem J. TerminalInput file: standard inputOutput file: standard inputTime limit: 2 secondsMemo ...
- Winter-1-D Max Sum 解题报告及测试数据
Time Limit:1000MS Memory Limit:32768KB Description Given a sequence a[1],a[2],a[3]......a[n], your j ...
- How can For each...
Answer: I understand the IEnumerator/IEnumerable methods and properties and also how they are inte ...
- WPS宏不可用解决方法
在使用WPS Office过程中,遇见宏不可用,在启用宏的过程中提示获取VBA插件 解决方法: 1.下载VBA插件,下载地址:https://pan.baidu.com/s/1LqBmXw37U0km ...
- 通过Java编码获取String分行字符串的内容
代码案列: import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.IOException ...
- debug教程
名称 解释 格式 a (Assemble) 逐行汇编 a [address] c (Compare) 比较两内存块 c range address d (Dump) 内存16进制显示 d [addre ...
- kernel command line 参数详解
Linux内核在启动的时候,能接收某些命令行选项或启动时参数.当内核不能识别某些硬件进而不能设置硬件参数或者为了避免内核更改某些参数的值,可以通过这种方式手动将这些参数传递给内核. 如果不使用启动管 ...
- 分析Ubuntu18.04启动后的各种任务
jello@jello:~$ ps -A PID TTY TIME CMD 1 ? 00:00:02 systemd 由idle进程(进程号为0的进程,那 ...
- RabbitMQ脑裂
在RabbitMQ3.4.x中会出现脑裂的现象,本文通过实验验证此脑裂现象,愿小伙伴们少走弯路. Preview 网上有两篇帖子(需要FQ) https://groups.google.com/for ...
- CSS样式遇见的问题总结记录
一.子元素都是浮动元素时,父元素最好是不用设置高度,防止子元素不设置高度溢出父元素 有时候会有零点几的误差高度 直接设置子元素高度即可 通过 clear: both;清除子元素浮动达到父元素自适应高度 ...