? POC(?? ??)??? Rust ??? ?? ??? ??? ????? C ?? ???? ????? ??? ?? ?? ??? ????.
Rust ??? ???: C?? ????? ???: C?? Rust??? ??? ??
??-
1. ??
- 2. ???
- 3. C? ??
-
-
3.1. ?? ?????? ???? ?? ??
- 3.2. Mutex? ??
4. ?????? ??
-
-
4.1. ?? ?? ??
- 4.2. Mutex ? Arc? ??? ??
- 4.3. ??? ? RwLock
5. ??
- 6. ????
1. ??
?????
???? ????? ??? ??? ??? ? ?? ?? ???? ??? ? ?????. ???? ???? ?? ??? ???? ??? ???? ? ? ??? ? ???, ? ??? ???? ??? ???? ??? ? ?? ?? ??? ?? ??? ?????.
2. ???
???? ??? ??? ??? ? ?? ?? ?????. ?? ???? ??? ??? ???? ???? ??? ???? ?? ???? ??? ? ????.
?? ???? ???? ?? ??? ?? ?? CPU ???? ??? ???? ???? ??? ? ????. ?? ?? ????? ?? ??? ????? ? ? ???? ???? ??? ?? ??? ?? ? ????.
3. C? ??
C?? ??? ???? ??? ?????.
- ?? ??? 1000???.
- ?? ?? ??? ? ? ?? ??? ?????.
- ???? ???? ??? ????? ?? ?????.
int saldo = 1000; void creditar(int valor) { int tmp_saldo = saldo; sleep(1); // Delay simulado saldo += tmp_saldo + valor; } void debitar(int valor) { int temp = saldo; sleep(1); // Delay simulado if (temp >= valor) { saldo = temp - valor; } } void* processar_transacao(void* arg) { int valor = *(int*)arg; if (valor > 0) { creditar(valor); } else { debitar(abs(valor)); } return NULL; } int main() { int transactions[] = {100, -50, 200, -150, 300, -200, 150, -100, 50, -50}; int num_transactions = sizeof(transactions) / sizeof(transactions[0]); pthread_t threads[num_transactions]; for (int i = 0; i < num_transactions; i++) { pthread_create(&threads[i], NULL, processar_transacao, &transactions[i]); // Cria uma thread para cada transa??o } for (int i = 0; i < num_transactions; i++) { pthread_join(threads[i], NULL); // Aguarda todas as threads terminarem } printf("Saldo final da conta: %d\n", saldo); return 0; }
????? ??? ?? ??? ???? ?? ??? ??? ? ???, 2?? ???? ??? ?? ????? ???? ?? ??? ?????. ? ??? ?? ? ???? ?? ? ????? ????? ?? ???? ???? ?? ??? ?????.
? ??? ?? ? ???? ???? ??? ???? ??? ????? ?? ??? ?????.
3.2. Mutex? ??
int saldo = 1000; void creditar(int valor) { int tmp_saldo = saldo; sleep(1); // Delay simulado saldo += tmp_saldo + valor; } void debitar(int valor) { int temp = saldo; sleep(1); // Delay simulado if (temp >= valor) { saldo = temp - valor; } } void* processar_transacao(void* arg) { int valor = *(int*)arg; if (valor > 0) { creditar(valor); } else { debitar(abs(valor)); } return NULL; } int main() { int transactions[] = {100, -50, 200, -150, 300, -200, 150, -100, 50, -50}; int num_transactions = sizeof(transactions) / sizeof(transactions[0]); pthread_t threads[num_transactions]; for (int i = 0; i < num_transactions; i++) { pthread_create(&threads[i], NULL, processar_transacao, &transactions[i]); // Cria uma thread para cada transa??o } for (int i = 0; i < num_transactions; i++) { pthread_join(threads[i], NULL); // Aguarda todas as threads terminarem } printf("Saldo final da conta: %d\n", saldo); return 0; }
???? ? ?? ??? ???? ?? ???? ???? ? ??? ???? ??? ?? ?????. ????? ??? "?? ??"? ???? ?? ?? ?? ???? ???????.
???? ???? ???? ??? ???? ????? ?? ???? ? ?? ???? ???? ??? ??? ?? ?????. ?? ? ? ??? ????(???)? ?? ???? ??? ????? ?? ?????.
4. ?????? ??
int saldo = 1000; pthread_mutex_t saldo_mutex; // Mutex para proteger o saldo void creditar(int valor) { pthread_mutex_lock(&saldo_mutex); // Bloqueia o mutex int tmp_saldo = saldo; sleep(1); // Delay simulado saldo = tmp_saldo + valor; pthread_mutex_unlock(&saldo_mutex); // Libera o mutex } void debitar(int valor) { pthread_mutex_lock(&saldo_mutex); // Bloqueia o mutex int tmp_saldo = saldo; sleep(1); // Delay simulado if (tmp_saldo >= valor) { saldo = tmp_saldo - valor; } pthread_mutex_unlock(&saldo_mutex); // Libera o mutex }
Rust? ??? ??? ?? ??? ???? ?? ????? ???, ???? ? ????? ??? ? ??? ??? ?? ??? ??? ?????? ??? ????? ??? ? ????.
Rust? ???, ?? ? ???? ??? ?? ? ?? ??? ???? ??? ?? ???? ?? ??
? ?????.- Arc: ?? ???? ???? ?????.
- Mutex ? RwLock: ?? ??? ???? ?? ??? ??
4.1. ?? ?? ??
Arc ? Mutex ???? ???? ??
Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — enabling you to eliminate many classes of bugs at compile-time.
Rust? ?? ?? ?? ????? ?? ??? ???(??)? ?? ????? ?? ???? ????.
??? ???? ?? ???? ?? ???(handle1 ? handle2)? ???? ??? ?????? ??? ?????.
???? ?? ???? ??? ????.
fn main() { let mut saldo = 1000; // saldo mutável, mas sem prote??o let handle1 = thread::spawn(move || { saldo += 100; // erro: `saldo` é movido para esta thread sem prote??o }); let handle2 = thread::spawn(move || { saldo -= 50; // erro: `saldo` é movido para esta thread sem prote??o }); handle1.join().unwrap(); handle2.join().unwrap(); }
4.2. Mutex ? Arc? ??? ??
Mutex? Arc? ???? ?? ?? ??? ????? ??? ????? ??? ? ?????.
error[E0382]: use of moved value: `saldo`
4.3. ??? ? RwLock
Mutex? RwLock? ?? ??? ???? ? ???? ?? ??? ?? ??? ????.
???: ? ???? ???? ?? ?? ???? ???? ?? ???? ??? ??? ?? ???? ???? ?????. ???? ?????? ??? ???? ????? ??? ?? ??????? ???? ?????.
RwLock: .read()? ???? ?? ?? ??? ???? .write()? ???? ?? ??? ?????. ?? ???? ?? ??? ???? ??? ?????? ??? ??? ????? ??????.
5. ??
C? Rust? ??? ?? ??? ???? ??? ?? ??? ?????. C??? ?? ?? ??? ???? ?? ??? ????? Rust? ??? ?? ??? Mutex, RwLock ? Arc? ?? ??? ?? ??? ??? ??? ??? ????. ?? ??? ?? ???? ?? ?? ???, ??? ??? ???? ?????? ??? ??? ?????
.????? Rust? ?? ???? ???? ??? ???? ???? ??? ?????.
6. ????
- ??? ??? ???: https://github.com/z4nder/rust-data-races
- https://en.wikipedia.org/wiki/Race_condition
- https://blog.bughunt.com.br/o-que-sao-vulnerabilidades-race-condition/
- https://medium.com/cwi-software/spring-boot-race-condition-e-ambiente-multi-thread-263b21e0042e
- https://learn.microsoft.com/en-us/troubleshoot/developer/visualstudio/visual-basic/??-compilers/race-conditions-deadlocks
- https://www.reddit.com/r/rust/comments/18faxjg/understanding_threadsafety_vs_race_conditions/?rdt=52263
- https://doc.rust-lang.org/nomicon/races.html
- https://news.ycombinator.com/item?id=23599598
? ??? Rust Threads ???: C?? ??.? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

STD :: Chrono? ?? ?? ??, ?? ?? ??, ?? ?? ? ?? ?? ? ?? ?? ??? ???? C?? ???? ??? ?????. 1. std :: chrono :: system_clock :: now ()? ???? ?? ??? ?? ? ??? ?? ??? ???? ?? ? ? ??? ??? ??? ???? ?? ?? ? ????. 2. std :: Chrono :: steady_clock? ???? ?? ??? ???? ?? ??? ???? duration_cast? ?? ?? ?, ? ? ?? ??? ??????. 3. ?? (time_point) ? ?? (??)? ?? ??? ? ? ??? ?? ??? ? ?? epoch (epoch)???? ???????.

C : 1?? ?? ????? ?? ??? ?? ??? ????. Linux ????? Backtrace ? Backtrace_symbols ??? ??????. ?? ?? ? ?? ?? ??? ???? ??? ? ? -rdynamic ?? ??? ???????. 2. Windows ????? CaptUreStackBackTrace ??? ???? DBGHELP.LIB? ???? PDB ??? ???? ?? ??? ?? ???????. 3. GoogleBreakPad ?? Boost.StackTrace? ?? ?? ?????? ???? ?? ??? ? ?? ?? ??? ??????. 4. ?? ???? ?? ??? ???? ?? ???? ?? ??? ???? ?????.

C??, POD (PANDALDATA) ??? ??? ??? ?? ??? ???? C ?? ??? ??? ?????. ??? ? ?? ??? ????????. ??? ??? ?? ???? ???, ?? memcpy? ?? ?? ? ? ????. ?? ????? ?? ??? ??? ??? ? ????. ?? ?? ???? ??? ?????. ?? ? ?? ??? ??, ??? ?? ??? ?? ???, ?? ?? ?? ?? ??? ?? ? ?? ? ?? ?? ??? ?????. ?? ?? structpoint {intx; inty;}? pod???. ??? ???? ???? I/O, C ?? ???, ?? ??? ?? ?????. std :: is_pod? ?? ??? POD?? ??? ? ??? C 11 ??? std :: is_trivia? ???? ?? ????.

C?? Python Code? ????? ?? ???? ??? ? ?? ???, ?? ?? ?? ??? ???? ?? ??? ?? ? ? ????. 1. Py_Initialize ()? ?????? ????? py_finalize ()? ????. 2. pyrun_simplefile? ???? ??? ?? ?? pyrun_simplefile? ?????. 3. pyimport_importmodule? ?? ?? ?? ??, pyobject_getattrstring? ?? ??? ???? py_buildvalue? ?? ??? ???? ??? ???? ???? ??

functionhiding alkes ressaMenaMeAsabaseClassFunctions? henaderivedClassDefinesAftunction, theBaseInAccessibleThroughTheDerivedClass.thishAppenswhentheBaseFunctionis notvirtualorsignaturesdon n'tmatchforevered, and nousingdeclarationis

C?? ??? ?? ??? ???? ? ?? ?? ??? ???? : ?? ??? ??, std :: ?? ? ?? ??? ? ??? ???. 1. ?? ???? ?? ???? ???? ??? ???? ?? C ?????? ????? ???? ?? ????. 2. STD :: LAMBDA ???? ?? ? ??? ?? C?? ???? ???? ??? ?? ??? ??? ???? ??-?????. 3. ??? ?? ??? ?? ???? ????? ?? ?? ?? ??? ????? ??? ??? ?? ??? ???? ? ????. ????? ???? ??? std :: ?? ?? ???? ?? ??????? ?? ???? ?? ?? ? ? ????.

anullpointerinc isaspecialValueindicating thatapointerspointtoanyvalidmorylocation, anditusiusedToSafelyManageNageanDcheckPointersbeforedEereferencing.1.Beforec 11,0ornull? WASSED, BUTNULLPTRISFREFERREDFORITYONDTYPESAFETY.SUNULLPOINTETYTETETENULUNULPENTETETETENGE

STD :: MOVE? ??? ???? ???? ?? ??? rvalue ??? ???? ????? ??? ?? ??? ??? ? ??? ?????. ?? ??, ??? ????? ? ???? ???? ???? ???? ?? ?? ??? ???? ?? ?? ?? ???? ?? ? ? ????. ?? ?? ??, ???? ?? ?? ??? ??? ?? ???? ???? ??? ?? ???? ?????? ???????. ??? ?? ????? ???? ???? ??? ????? ???, ?? ?? ??? ??? ?? ???? ????. ??? ????? ?? ? ? ??? ?? ???? ??? ?? ? ??? ??? ?? ??? ???? ?? RVO ???? ?? ??? ? ???? STD :: ??? ???? ???? ??? ? ? ????. ??? ???? ?? ???? ??? ?????? ??, ???? ??? ? ??? ??? ??? ?? ??? ?????.
