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
//! # Thread-Share
//!
//! A comprehensive Rust library for safe and efficient data exchange between threads.
//!
//! ## ๐ Features
//!
//! - **Simple API** for data exchange between threads
//! - **Automatic synchronization** and thread safety
//! - **Support for any data types** that implement `Clone`
//! - **Efficient synchronization** using `parking_lot`
//! - **Multiple complexity levels**: from simple locks to zero-copy atomic operations
//! - **Enhanced thread management** with automatic spawning and joining
//! - **Smart macros** for cleaner thread management syntax
//! - **Real-world examples** including HTTP server and socket client
//!
//! ## ๐ฏ Quick Start
//!
//! ```rust
//! use thread_share::share;
//!
//! // Create shared data
//! let counter = share!(0);
//! let clone = counter.clone();
//!
//! // Spawn a simple thread
//! std::thread::spawn(move || {
//! clone.update(|x| *x = *x + 1);
//! });
//!
//! // Wait a bit and check result
//! std::thread::sleep(std::time::Duration::from_millis(100));
//! println!("Final value: {}", counter.get());
//! ```
//!
//! ## ๐๏ธ Architecture Overview
//!
//! The library provides several levels of abstraction:
//!
//! ### ๐ Lock-Based (Safe & Predictable)
//! - **`ThreadShare<T>`** - Full-featured synchronization with change detection
//! - **`SimpleShare<T>`** - Lightweight alternative for basic use cases
//! - **`ArcThreadShareLocked<T>`** - Safe zero-copy with read/write locks
//!
//! ### โก Atomic-Based (High Performance)
//! - **`ArcThreadShare<T>`** - Zero-copy atomic operations (use with caution)
//!
//! ### ๐งต Enhanced Management
//! - **`EnhancedThreadShare<T>`** - Automatic thread spawning and joining
//! - **`ThreadManager`** - Standalone thread management utility
//!
//! ## ๐ Core Concepts
//!
//! ### ThreadShare<T> - Main Structure
//! Provides comprehensive thread synchronization with automatic cloning:
//!
//! ```rust
//! use thread_share::share;
//!
//! let data = share!(vec![1, 2, 3]);
//! let clone = data.clone();
//!
//! // Thread 1
//! std::thread::spawn(move || {
//! clone.update(|v| v.push(4));
//! });
//!
//! // Thread 2 (main)
//! data.wait_for_change_forever();
//! println!("Updated: {:?}", data.get());
//! ```
//!
//! ### ThreadShare<T> - Basic Management
//! Simple thread management with manual control:
//!
//! ```rust
//! use thread_share::share;
//!
//! let data = share!(vec![1, 2, 3]);
//! let clone = data.clone();
//!
//! std::thread::spawn(move || {
//! clone.update(|d| d.push(4));
//! });
//!
//! std::thread::sleep(std::time::Duration::from_millis(100));
//! println!("Updated: {:?}", data.get());
//! ```
//!
//! ## ๐ง Usage Patterns
//!
//! ### Serialization Support (Optional Feature)
//!
//! The library provides optional serialization support through the `serialize` feature:
//!
//! ```bash
//! # Enable serialization support
//! cargo add thread-share --features serialize
//! ```
//!
//! ```rust
//! use thread_share::ThreadShare;
//!
//! // Note: This example requires the 'serialize' feature
//! let data = ThreadShare::new(vec![1, 2, 3]);
//!
//! // Serialize to JSON (available with serialize feature)
//! // let json = data.to_json().expect("Failed to serialize");
//! // println!("JSON: {}", json); // Output: [1,2,3]
//!
//! // Deserialize from JSON (available with serialize feature)
//! // data.from_json("[4,5,6]").expect("Failed to deserialize");
//! // assert_eq!(data.get(), vec![4, 5, 6]);
//! ```
//!
//! **Note**: Serialization methods are only available when the `serialize` feature is enabled.
//!
//! ### Basic Data Sharing
//! ```rust
//! use thread_share::share;
//!
//! let counter = share!(0);
//! let clone = counter.clone();
//!
//! std::thread::spawn(move || {
//! for _ in 0..100 {
//! clone.update(|x| *x += 1);
//! }
//! });
//!
//! std::thread::sleep(std::time::Duration::from_millis(100));
//! println!("Counter: {}", counter.get());
//! ```
//!
//! ### Zero-Copy Operations
//! ```rust
//! use thread_share::{share, ArcThreadShare};
//!
//! let data = share!(String::from("Hello"));
//! let arc_data = data.as_arc();
//! let arc_share = ArcThreadShare::from_arc(arc_data);
//!
//! // Use atomic operations for performance
//! arc_share.update(|s| s.push_str(" World"));
//! ```
//!
//! ### Enhanced Thread Management
//! ```rust
//! use thread_share::{enhanced_share, spawn_workers};
//!
//! let data = enhanced_share!(vec![1, 2, 3]);
//!
//! spawn_workers!(data, {
//! processor: |data| { data.update(|v| v.sort()); },
//! validator: |data| { assert!(data.get().is_sorted()); }
//! });
//!
//! data.join_all().expect("Failed to join");
//! ```
//!
//! ## โ ๏ธ Important Notes
//!
//! ### ArcThreadShare Limitations
//! - **Complex operations are not atomic** - use `increment()` and `add()` methods
//! - **High contention can cause lost updates** - test with realistic load
//! - **Memory allocation overhead** per operation
//!
//! ### Best Practices
//! - Use **`ThreadShare<T>`** for most applications
//! - Use **`ArcThreadShare<T>`** only when you understand its limitations
//! - Use **`EnhancedThreadShare<T>`** for simplified thread management
//! - Always test with realistic contention levels
//!
//! ## ๐ Examples
//!
//! Check the `examples/` directory for complete working examples:
//!
//! - **`basic_usage.rs`** - Fundamental concepts
//! - **`http_integration_helpers.rs`** - Complete HTTP server
//! - **`socket_client_usage.rs`** - Enhanced socket client
//! - **`atomic_usage.rs`** - Zero-copy patterns
//!
//! ## ๐งช Testing
//!
//! Run the comprehensive test suite:
//!
//! ```bash
//! cargo test # All tests
//! cargo test --test core_tests # Specific test file
//! cargo test -- --nocapture # With output
//! ```
//!
//! ## ๐ License
//!
//! This project is licensed under the MIT License.
//!
//! ## ๐ค Contributing
//!
//! Contributions are welcome! Please feel free to submit a Pull Request.
// Re-export main structures
pub use ArcThreadShare;
pub use ;
pub use EnhancedThreadShare;
pub use ArcThreadShareLocked;
pub use ThreadManager;