Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Events
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
forspy
Win32
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
Win32
/
teach_win32_Process_Thread2.cpp
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
85 lines (83 loc) · 2 KB
master
Breadcrumbs
Win32
/
teach_win32_Process_Thread2.cpp
Copy path
Top
File metadata and controls
Code
Blame
85 lines (83 loc) · 2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// teach_win32_Process_Thread2.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<Windows.h>//创建线程用
//线程(函数)由两个部分组成
//1.操作系统内核对象 用来存放线程统计信息和管理
//2.线程堆栈 用来维护线程在执行代码中需要的函数参数和局部变量
//进程必然带一个入口函数,即主线程
//线程的生命周期会受到进程的影响,进程一旦死亡,里面的所有线程都会死亡
//线程的开销比进程少,进程是不活跃的,如果要去异步解决问题 尽量创建线程而不是进程
//魔兽世界跑地图会加载地图,加载玩家,需要去异步解决问题就是多线程
int ticket = 10;//定义十张票,
void func1(LPVOID param);
void func2(LPVOID param);
int main()
{
//创建一个线程
int index = 1;
HANDLE handle1=CreateThread(nullptr,//指向一个结构LPSECURITY_ATTRIBUTES ,必须为空
0,//新线程的初始化堆栈大小,默认0
(LPTHREAD_START_ROUTINE)func1,//对应的线程函数名
(LPVOID)index,//传给线程(回调)函数的参数
0,//线程的参数
nullptr//线程的ID,因为设置了句柄,所以不需要
);
index++;
HANDLE handle2 = CreateThread(nullptr,0,(LPTHREAD_START_ROUTINE)func2,(LPVOID)index,0,nullptr);
while (1)
{
if (ticket > 0)
{
printf("火车站卖出%d张票\n", ticket);
ticket--;
}
else
{
break;
}
}
//线程的三种退出
//1.线程函数的主动返回(推荐)
//2.在主线程使用TerminateThread结束指定的子线程
//TerminateThread(handle1, 0);//可能会导致资源释放不彻底
//3.调用ExitThread结束
//ExitThread(0);
return 0;//代表进程死亡
}
void func1(LPVOID param)
{
while (1)
{
if (ticket > 0)
{
printf("第%d代售点,卖出%d张票\n", (int)param, ticket);
ticket--;
}
else
{
break;
}
}
}
void func2(LPVOID param)
{
while (1)
{
if (ticket > 0)
{
printf("第%d代售点,卖出%d张票\n", (int)param, ticket);
ticket--;
}
else
{
break;
}
}
}
////把10从内存复制到cpu寄存器 2.减减变为9 3.再从cpu寄存器复制到内存
//各个线程都可以抢占cpu,假如线程1抢占到了cpu,线程1需要打印printf() 但是这个是需要时间运算的
//然后需要--,这时候量程的时间片到了,--操作被终止
//接着比如被线程2抢到了 线程2打印 然后--的时候时间到了
//线程1抢到了 继续执行--
//所以需要线程间的通讯,线程的互斥
You can’t perform that action at this time.