FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpp-nodejs-shared-memory-ipc/example2/cpp-writer/main.cpp at main · 1010code/cpp-nodejs-shared-memory-ipc · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
1010code
/
cpp-nodejs-shared-memory-ipc
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cpp-nodejs-shared-memory-ipc
/
example2
/
cpp-writer
/
main.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
executable file
·
74 lines (55 loc) · 2.02 KB
Breadcrumbs
cpp-nodejs-shared-memory-ipc
/
example2
/
cpp-writer
/
main.cpp
Copy path
File metadata and controls
executable file
·
74 lines (55 loc) · 2.02 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
//
main.cpp
/*
*
// 僅寫一次固定數值
#include "shared_memory_writer.h"
#include <iostream>
#include <vector>
int main() {
const std::string name = "/my_shared_memory"; // 共享記憶體名稱
const size_t dataSize = 2; // 要寫入的浮點數數量
// 創建共享記憶體寫入器
SharedMemoryWriter writer(name, dataSize);
// 要寫入的浮點數數據
std::vector<float> data = {1.55f, 2.66f};
// 寫入共享記憶體
if (!writer.writeData(data)) {
std::cerr << "寫入共享記憶體失敗" << std::endl;
return 1;
}
return 0;
}
*
*/
#
include
"
shared_memory_writer.h
"
#
include
<
iostream
>
#
include
<
vector
>
#
include
<
cstdlib
>
//
用於 rand() 函數
#
include
<
ctime
>
//
用於 time() 函數
#
include
<
chrono
>
#
include
<
thread
>
/*
*
本程式會每100ms隨機寫dataSize個數值
*
*/
int
main
() {
const
std::string name =
"
/my_shared_memory
"
;
//
共享記憶體名稱
const
size_t
dataSize =
2
;
//
要寫入的浮點數數量(2 個)
float
data[dataSize] = {
0.0
};
//
創建共享記憶體寫入器
SharedMemoryWriter
writer
(name,
sizeof
(data), data);
//
初始化隨機數生成器
std::srand
(
static_cast
<
unsigned
int
>(
std::time
(
nullptr
)));
while
(
true
) {
//
產生兩個隨機浮點數
data[
0
] =
static_cast
<
float
>(
std::rand
()) / (
static_cast
<
float
>(
RAND_MAX
/
100
.
0f
));
data[
1
] =
static_cast
<
float
>(
std::rand
()) / (
static_cast
<
float
>(
RAND_MAX
/
100
.
0f
));
//
寫入共享記憶體
if
(!writer.
writeData
()) {
std::cerr <<
"
寫入共享記憶體失敗
"
<< std::endl;
return
1
;
}
//
輸出已寫入的數據
std::cout <<
"
已寫入隨機浮點數:
"
<< data[
0
] <<
"
,
"
<< data[
1
] << std::endl;
//
每隔 100 毫秒執行一次
std::this_thread::sleep_for
(
std::chrono::milliseconds
(
100
));
}
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL