forked from Bob0817912/Visual_Assessment_Tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fe1013e
commit a29dbe3
Showing
2 changed files
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* 1. 多次编译运行,观察输出结果。 | ||
* 2. 修改下面的代码,使得 a 的值等于 threadNum * 10000。只能修改 foo 函数,不能修改 main 函数,可以增加变量。 | ||
*/ | ||
|
||
#include <iostream> | ||
#include <thread> | ||
#include <vector> | ||
|
||
const int threadNum = 10; // 线程数 | ||
|
||
void foo(int &a) { | ||
for (int i = 0; i < 10000; i++) // 每个线程对 a 加 10000 次 | ||
a++; | ||
return; | ||
} | ||
|
||
int main(int argc, char* argv[], char** env) | ||
{ | ||
int a = 0; // 总计数器 | ||
std::vector<std::thread> threadVec; | ||
|
||
for (int i = 0; i < threadNum; i++) // 创建 threadNum 个线程 | ||
threadVec.push_back(std::thread(foo, std::ref(a))); | ||
for (int i = 0; i < threadNum; i++) | ||
threadVec[i].join(); // 等待所有线程结束 | ||
|
||
std::cout << "a = " << a << std::endl; // 输出 a 的值 | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,7 @@ | |
|
||
以下为可选: | ||
|
||
+ 修改 `thread/thread.cpp` 中的代码 | ||
+ 实现一个进程间通信(IPC)框架 | ||
+ 压缩一张位图 | ||
+ 实现一个坐标系转换类 | ||
|