thread-share 0.1.6

A Rust library for safe and efficient data sharing between threads with zero-copy operations, change detection, and enhanced thread management.
Documentation
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
[![Crates.io](https://img.shields.io/crates/v/thread-share?style=for-the-badge)](https://crates.io/crates/thread-share)
[![Documentation](https://img.shields.io/badge/docs-docs.rs-blue?style=for-the-badge)](https://docs.rs/thread-share/)
[![GitHub issues](https://img.shields.io/badge/github-issues-orange?style=for-the-badge)](https://github.com/s00d/thread-share/issues)
[![GitHub stars](https://img.shields.io/badge/github-stars-yellow?style=for-the-badge)](https://github.com/s00d/thread-share/stargazers)
[![Donate](https://img.shields.io/badge/Donate-Donationalerts-ff4081?style=for-the-badge)](https://www.donationalerts.com/r/s00d88)

# Thread-Share

> **"I got tired of playing around with data passing between threads and decided to write this library"**

A powerful Rust library for safe data exchange between threads with automatic thread management.

## ๐ŸŽฏ What Problem Does This Solve?

Working with shared data between threads in Rust is often frustrating:
- Manual `Arc<Mutex<T>>` or `Arc<RwLock<T>>` combinations
- Complex ownership patterns
- Boilerplate code for every thread-safe structure
- Manual thread spawning and joining

**Thread-Share** provides a simple API that handles all the complexity for you.

## ๐Ÿš€ Features

- **๐Ÿ”’ Thread-Safe**: Built-in synchronization with `RwLock` and `AtomicPtr`
- **โšก High Performance**: Efficient `parking_lot` synchronization primitives
- **๐Ÿงต Automatic Thread Management**: Spawn and manage multiple threads with one macro
- **๐Ÿ“ฆ Zero-Copy**: Support for working without cloning data between threads
- **๐Ÿ”„ Change Detection**: Built-in waiting mechanisms for data changes
- **โœจ Macro Support**: Convenient macros for quick setup
- **๐Ÿ“„ Serialization Support**: JSON serialization for all types with `serialize` feature

## ๐Ÿ“ฆ Installation

```bash
# Basic installation
cargo add thread-share

# With serialization support
cargo add thread-share --features serialize
```

## ๐Ÿš€ Quick Start

### Basic Usage with Thread Management

```rust
use thread_share::{share, enhanced_share, spawn_workers};
use std::time::Duration;

fn main() {
    // Create shared counter
    let counter = enhanced_share!(0);
    
    // Spawn multiple workers with one macro call
    let manager = spawn_workers!(counter, {
        incrementer: |counter| {
            for i in 1..=10 {
                counter.set(i);
                std::thread::sleep(Duration::from_millis(100));
            }
        },
        monitor: |counter| {
            for _ in 0..10 {
                println!("Value: {}", counter.get());
                std::thread::sleep(Duration::from_millis(200));
            }
        }
    });
    
    // Main thread reads values
    while counter.get() < 10 {
        println!("Counter: {}", counter.get());
        std::thread::sleep(Duration::from_millis(150));
    }
    
    // Add additional worker dynamically
    let counter_clone = counter.clone();
    let additional_worker = std::thread::spawn(move || {
        for _ in 0..3 {
            counter_clone.update(|x| *x *= 2);
            std::thread::sleep(Duration::from_millis(300));
        }
    });
    
    manager.add_worker("multiplier", additional_worker)?;
    
    // Demonstrate worker control
    println!("Active workers: {}", manager.active_workers());
    println!("Worker names: {:?}", manager.get_worker_names());
    
    // Pause incrementer worker temporarily
    manager.pause_worker("incrementer")?;
    println!("Incrementer paused for 1 second");
    std::thread::sleep(Duration::from_secs(1));
    
    // Resume incrementer worker
    manager.resume_worker("incrementer")?;
    println!("Incrementer resumed");
    
    // Remove monitor worker early
    manager.remove_worker("monitor")?;
    println!("Monitor worker stopped and removed");
    
    // Stop all remaining workers at once
    manager.remove_all_workers()?;
    println!("All workers stopped");
    
    // Check final state
    println!("Final active workers: {}", manager.active_workers());
    
    // Wait for remaining workers to complete
    manager.join_all().expect("Failed to join threads");
}
```

## ๐Ÿ”ง Core Types

### 1. ThreadShare<T> - Basic Thread-Safe Data

```rust
use thread_share::share;

let data = share!(vec![1, 2, 3]);

// Basic operations
data.set(vec![4, 5, 6]);           // Set new value
let value = data.get();             // Get copy of data
data.update(|v| v.push(7));        // Update in place

// Thread-safe operations
data.wait_for_change_forever();     // Wait for changes
data.wait_for_change(timeout);      // Wait with timeout
```

### 2. EnhancedThreadShare<T> - Automatic Thread Management

```rust
use thread_share::{enhanced_share, spawn_workers};

let data = enhanced_share!(0);

// Spawn multiple threads with one macro
let manager = spawn_workers!(data, {
    worker1: |data| { /* thread logic */ },
    worker2: |data| { /* thread logic */ }
});

// Automatic thread joining
manager.join_all().expect("Failed to join threads");
```

### 3. WorkerManager - Fine-Grained Control

```rust
let manager = spawn_workers!(data, {
    worker: |data| { /* logic */ }
});

// Control individual workers
manager.pause_worker("worker")?;
manager.resume_worker("worker")?;
manager.remove_worker("worker")?;

// Monitor workers
println!("Active workers: {}", manager.active_workers());
println!("Worker names: {:?}", manager.get_worker_names());
```

#### Creating WorkerManager Directly

```rust
use thread_share::worker_manager::WorkerManager;
use std::thread;
use std::time::Duration;

fn main() {
    // Create empty manager
    let manager = WorkerManager::new();
    
    // Add workers programmatically
    let handle1 = thread::spawn(|| {
        for i in 1..=5 {
            println!("Worker 1: {}", i);
            thread::sleep(Duration::from_millis(100));
        }
    });
    
    let handle2 = thread::spawn(|| {
        for i in 1..=3 {
            println!("Worker 2: {}", i);
            thread::sleep(Duration::from_millis(150));
        }
    });
    
    // Add workers to manager
    manager.add_worker("worker1", handle1)?;
    manager.add_worker("worker2", handle2)?;
    
    println!("Active workers: {}", manager.active_workers());
    println!("Worker names: {:?}", manager.get_worker_names());
    
    // Wait for completion
    manager.join_all()?;
}
```

#### Creating WorkerManager with Existing Threads

```rust
use thread_share::{enhanced_share, worker_manager::WorkerManager};

fn main() {
    let data = enhanced_share!(0);
    
    // Get existing threads from EnhancedThreadShare
    let existing_threads = data.get_threads();
    
    // Create manager with existing threads
    let manager = WorkerManager::new_with_threads(existing_threads);
    
    // Add new worker
    let data_clone = data.clone();
    let new_worker = thread::spawn(move || {
        for _ in 0..3 {
            data_clone.update(|x| *x += 10);
            thread::sleep(Duration::from_millis(200));
        }
    });
    
    manager.add_worker("additional_worker", new_worker)?;
    
    // Monitor and control
    println!("Active workers: {}", manager.active_workers());
    manager.join_all()?;
}
```

#### Worker State Management

```rust
use thread_share::{enhanced_share, spawn_workers};

fn main() {
    let data = enhanced_share!(0);
    
    let manager = spawn_workers!(data, {
        counter: |data| {
            for i in 1..=10 {
                data.set(i);
                thread::sleep(Duration::from_millis(500));
            }
        },
        monitor: |data| {
            for _ in 0..10 {
                println!("Value: {}", data.get());
                thread::sleep(Duration::from_millis(1000));
            }
        }
    });
    
    // Control worker states
    thread::sleep(Duration::from_secs(2));
    
    // Pause counter worker
    manager.pause_worker("counter")?;
    println!("Counter worker paused");
    
    thread::sleep(Duration::from_secs(1));
    
    // Resume counter worker
    manager.resume_worker("counter")?;
    println!("Counter worker resumed");
    
    // Remove monitor worker
    manager.remove_worker("monitor")?;
    println!("Monitor worker removed");
    
    // Wait for remaining workers
    manager.join_all()?;
}
```

## ๐Ÿ“š Examples

### Producer-Consumer Pattern

```rust
use thread_share::{enhanced_share, spawn_workers};

fn main() {
    let queue = enhanced_share!(Vec::<String>::new());
    
    // Spawn producer and consumer threads
    let manager = spawn_workers!(queue, {
        producer: |queue| {
            for i in 0..5 {
                queue.update(|q| q.push(format!("Message {}", i)));
                std::thread::sleep(Duration::from_millis(100));
            }
        },
        consumer: |queue| {
            let mut processed = 0;
            while processed < 5 {
                let messages = queue.get();
                if !messages.is_empty() {
                    queue.update(|q| {
                        if let Some(msg) = q.pop() {
                            println!("Processed: {}", msg);
                            processed += 1;
                        }
                    });
                } else {
                    std::thread::sleep(Duration::from_millis(50));
                }
            }
        }
    });
    
    // Wait for all workers
    manager.join_all().expect("Failed to join threads");
}
```

### HTTP Server with Visit Counter

```rust
use thread_share::{enhanced_share, spawn_workers};
use std::net::{TcpListener, TcpStream};
use std::io::{Read, Write};

fn main() {
    let visits = enhanced_share!(0);
    
    // Spawn server thread
    let manager = spawn_workers!(visits, {
        server: |visits| {
            let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
            
            for stream in listener.incoming() {
                let mut stream = stream.unwrap();
                let mut buffer = [0; 1024];
                
                stream.read(&mut buffer).unwrap();
                
                // Increment visit counter for main page requests
                if buffer.starts_with(b"GET / ") {
                    visits.update(|v| *v += 1);
                }
                
                let response = "HTTP/1.1 200 OK\r\n\r\nHello World!";
                stream.write(response.as_bytes()).unwrap();
            }
        }
    });
    
    // Monitor visits
    for _ in 0..10 {
        std::thread::sleep(Duration::from_secs(1));
        println!("Total visits: {}", visits.get());
    }
}
```

### Dynamic Worker Management

```rust
use thread_share::{enhanced_share, spawn_workers, worker_manager::WorkerManager};

fn main() {
    let data = enhanced_share!(0);
    
    // Start with basic workers
    let manager = spawn_workers!(data, {
        counter: |data| {
            for i in 1..=5 {
                data.set(i);
                std::thread::sleep(Duration::from_millis(500));
            }
        }
    });
    
    // Add new worker dynamically
    let data_clone = data.clone();
    let new_worker = std::thread::spawn(move || {
        for _ in 0..3 {
            data_clone.update(|x| *x *= 2);
            std::thread::sleep(Duration::from_millis(300));
        }
    });
    
    manager.add_worker("multiplier", new_worker)?;
    
    // Control workers
    manager.pause_worker("counter")?;
    std::thread::sleep(Duration::from_secs(1));
    manager.resume_worker("counter")?;
    
    // Wait for all
    manager.join_all()?;
}
```

### Custom Types with Change Detection

```rust
use thread_share::{enhanced_share, spawn_workers};

#[derive(Clone, Debug)]
struct User {
    name: String,
    age: u32,
    is_online: bool,
}

fn main() {
    let user = enhanced_share!(User {
        name: "Alice".to_string(),
        age: 25,
        is_online: true,
    });
    
    // Spawn multiple workers that update user data
    let manager = spawn_workers!(user, {
        age_updater: |user| {
            for _ in 0..5 {
                user.update(|u| u.age += 1);
                std::thread::sleep(Duration::from_millis(200));
            }
        },
        status_toggler: |user| {
            for _ in 0..5 {
                user.update(|u| u.is_online = !u.is_online);
                std::thread::sleep(Duration::from_millis(300));
            }
        },
        monitor: |user| {
            for _ in 0..10 {
                let current = user.get();
                println!("User: {} ({}), Age: {}, Online: {}", 
                    current.name, current.age, current.age, current.is_online);
                std::thread::sleep(Duration::from_millis(500));
            }
        }
    });
    
    // Wait for all workers
    manager.join_all().expect("Failed to join threads");
    
    let final_user = user.get();
    println!("Final state: {:?}", final_user);
}
```

## ๐Ÿ” When to Use Each Type

| Type | Use Case | Description |
|------|----------|-------------|
| **ThreadShare** | Simple data sharing | Basic thread-safe data with manual thread management |
| **EnhancedThreadShare** | Multi-threaded apps | Automatic thread spawning and joining |
| **WorkerManager** | Complex workflows | Fine-grained control over individual workers |

## โš ๏ธ Important Notes

### ArcThreadShare<T> - Use with Caution

```rust
use thread_share::ArcThreadShare;

let data = ArcThreadShare::new(0);

// โš ๏ธ Complex operations are NOT atomic
data.update(|x| *x += 1);  // May lose updates under high contention

// โœ… Use atomic methods instead
data.increment();           // Atomic increment
data.add(5);               // Atomic addition
```

**When NOT to use ArcThreadShare:**
- High-frequency updates (>1000 ops/second)
- Critical data integrity requirements
- Predictable performance needs

## ๐Ÿงช Running Examples

```bash
# Basic examples
cargo run --example basic_usage
cargo run --example atomic_usage

# Advanced examples with WorkerManager
cargo run --example worker_management
cargo run --example http_integration_helpers
cargo run --example socket_client_usage

# Run tests
cargo test
```

## ๐Ÿ”ง Requirements

- **Rust**: 1.85.0 or higher
- **Dependencies**: `parking_lot` (required), `serde` (optional)

## ๐Ÿ“„ License

MIT License - see [LICENSE](LICENSE) file for details.

## ๐Ÿค Contributing

Contributions welcome! Please submit a Pull Request.

## ๐Ÿ“š More Examples

Check the `examples/` directory for complete working examples:
- HTTP servers with Tokio and async-std
- Socket client with Node.js server
- Advanced worker management patterns
- Performance benchmarks