多线程
1
载入中...
搜索中...
未找到
main.cpp
浏览该文件的文档.
1
//
2
// main.cpp
3
// 多线程
4
//
5
// Created by 赵明明 on 2023/6/13.
6
//
7
#include "
FillPology.hpp
"
8
#include "
测试.hpp
"
9
#include <iostream>
10
#include <pthread.h>
11
#include <sys/time.h>
12
#include <chrono>
13
#include <thread>
14
15
using namespace
std;
16
17
#define NUM_THREADS 1
18
/*struct timeval{
19
long tv_sec;
20
long tv_usec;
21
};*/
22
矩阵
*
img
=
new
矩阵
(301,301,(
float
)0);
23
24
void
*
say_hello
(
void
* args)
25
{
26
int
tid = *((
int
*)args);
27
//std::cout <<"Hello Runoob! 线程 ID为: "<< tid << std::endl;
28
std::cout<<tid<<endl;
29
//test_FillPology();
30
//test_DrawPlane(img);
31
//pthread_exit(NULL);
32
return
0;
33
}
34
int
test_multi_thread
(
void
){
35
// insert code here...
36
//clock_t start=clock();
37
struct
timeval t1,t2;
38
gettimeofday(&t1,NULL);
39
pthread_t tids[
NUM_THREADS
];
40
int
indexes[
NUM_THREADS
];
41
int
i;
42
for
(i=0;i<
NUM_THREADS
; i++){
43
//cout <<"main():创建线程,"<<i<<endl;
44
45
indexes[i]=i;
46
int
ret=pthread_create(&tids[i],NULL,
say_hello
,&indexes[i]);
47
if
(ret!=0){
48
cout<<
"无法创建线程,"
<<ret<<endl;
49
exit(-1);
50
}
51
52
}
53
54
55
//pthread_exit(NULL);
56
//clock_t end=clock();
57
gettimeofday(&t2,NULL);
58
double
duration = (t2.tv_sec - t1.tv_sec) + (
double
)(t2.tv_usec-t1.tv_usec)/1000000.0;
59
//cout<<"multi_thread_donw:"<<duration<<"s"<<endl;;
60
pthread_exit(NULL);
61
//cout<<"multi_thread_donw:"<<((double)(end-start)/CLOCKS_PER_SEC)<<"s"<<endl;;
62
return
0;
63
}
64
65
// 一个虚拟函数
66
void
foo
(
int
Z)
67
{
68
for
(
int
i = 0; i < Z; i++) {
69
cout <<i<<
"线程使用函数指针作为可调用参数\n"
;
70
}
71
}
72
void
test_thread_object
(
void
){
73
/*
74
thread th1(foo,2);
75
cout<<th1.hardware_concurrency()<<endl;
76
cout<<"\n";
77
cout<<th1.native_handle()<<endl;
78
//th1.join();
79
*/
80
struct
timeval t1,t2;
81
gettimeofday(&t1,NULL);
82
thread threads[
NUM_THREADS
];
83
for
(
int
i=0;i<
NUM_THREADS
;++i){
84
threads[i] = thread(
test_DrawPlane
,
img
);
85
}
86
for
(
auto
&thread:threads)
87
thread.join();
88
gettimeofday(&t2,NULL);
89
double
duration = (t2.tv_sec - t1.tv_sec) + (
double
)(t2.tv_usec-t1.tv_usec)/1000000.0;
90
91
cout<<
"All threads joined!,timecost:"
<<duration <<
" s"
<<endl;;
92
}
93
94
95
int
main
(
int
argc,
const
char
* argv[]) {
96
//test_DrawPlane();
97
//test_FillPology();
98
//test_单位化();
99
//test_和();
100
//test_minmax();
101
//test_线();
102
//test_add_sub();
103
104
//test_矩阵();
105
/*test_c_();
106
test_r_();
107
*/
108
//test_子矩阵();
109
110
//test_multi_thread();
111
test_thread_object
();
112
113
int
numbers[5];
114
std::cout<<
"Enter five integers: "
;
115
for
(
int
i =0;i<5;i++){
116
std::cin >> numbers[i];
117
}
118
std::sort(numbers,numbers+5);
119
int
max=numbers[4];
120
int
min=numbers[0];
121
std::cout<<
"the maximum is :"
<<max<<
",minimum is :"
<<min<<endl;
122
return
0;
123
124
}
FillPology.hpp
test_DrawPlane
void test_DrawPlane(矩阵 *img)
Definition
测试.cpp:11
测试.hpp
矩阵
基本的矩阵计算类,用于完成各种矩阵初始化、最大最小、取值、加减乘除、精度设置等矩阵计算
Definition
矩阵.hpp:25
say_hello
void * say_hello(void *args)
Definition
main.cpp:24
img
矩阵 * img
Definition
main.cpp:22
test_multi_thread
int test_multi_thread(void)
Definition
main.cpp:34
foo
void foo(int Z)
Definition
main.cpp:66
NUM_THREADS
#define NUM_THREADS
Definition
main.cpp:17
test_thread_object
void test_thread_object(void)
Definition
main.cpp:72
main
int main(int argc, const char *argv[])
Definition
main.cpp:95
多线程
main.cpp
制作者
1.9.7