Archives
Trending
Support
Login
clear text
XML
Django
JavaScript
MATLAB
C
C++
C#
Python
SQL
Shell
Bash
Markdown
YAML
JSON
HTML
CSS
PHP
Java
Ruby
Go
Rust
Swift
Kotlin
Arduino
TypeScript
Perl
Autohotkey
Lua
SQF
R
Scala
Haskell
Groovy
Dart
Clojure
VB.NET
Objective-C
PowerShell
Bash
CoffeeScript
Verilog
#include
#include
#include
// Untuk exit, dll. // 1. Deklarasi Mutex Global pthread_mutex_t lock; int counter = 0; void* increment(void* arg) { for (int i = 0; i < 100000; i++) { // 2. Kunci (Lock) Mutex sebelum mengakses critical section pthread_mutex_lock(&lock); // Critical Section (Operasi yang harus atomik) counter++; // 3. Buka Kunci (Unlock) Mutex setelah selesai pthread_mutex_unlock(&lock); } return NULL; } int main() { pthread_t t1, t2; // 4. Inisialisasi Mutex if (pthread_mutex_init(&lock, NULL) != 0) { printf("\nMutex init failed\n"); return 1; } pthread_create(&t1, NULL, increment, NULL); pthread_create(&t2, NULL, increment, NULL); pthread_join(t1, NULL); pthread_join(t2, NULL); // 5. Hancurkan Mutex setelah tidak digunakan lagi pthread_mutex_destroy(&lock); printf("Final counter: %d\n", counter); // Output yang diharapkan dan seharusnya konsisten adalah: 200000 return 0; }
Mark as private (unlisted)
for 30 minutes
for 6 hours
for 1 day
for 1 week
for 1 month
for 1 year
everlasting (like CCCP)