MCPcopy Create free account
hub / github.com/apache/brpc / thread_worker

Function thread_worker

example/couchbase_c++/multithreaded_couchbase_client.cpp:171–232  ·  view source on GitHub ↗

Simple thread worker function

Source from the content-addressed store, hash-verified

169
170// Simple thread worker function
171void* thread_worker(void* arg) {
172 ThreadArgs* args = static_cast<ThreadArgs*>(arg);
173
174 std::cout << CYAN << "Thread " << args->thread_id << " starting on bucket "
175 << args->bucket_name << RESET << std::endl;
176
177 // Create CouchbaseOperations instance for this thread
178 brpc::CouchbaseOperations couchbase_ops;
179
180 // Authentication
181 brpc::CouchbaseOperations::Result auth_result = couchbase_ops.authenticate(
182 g_config.username, g_config.password, "127.0.0.1:11210", args->bucket_name);
183 // for SSL authentication use below line instead
184 // brpc::CouchbaseOperations::Result auth_result = couchbase_ops.authenticateSSL(username, password, "127.0.0.1:11210", args->bucket_name, "/path/to/cert.txt");
185
186 if (!auth_result.success) {
187 std::cout << RED << "Thread " << args->thread_id << ": Auth failed - "
188 << auth_result.error_message << RESET << std::endl;
189 return NULL;
190 }
191
192 // // Select bucket
193 // brpc::CouchbaseOperations::Result bucket_result =
194 // couchbase_ops.selectBucket(args->bucket_name);
195
196 // if (!bucket_result.success) {
197 // std::cout << RED << "Thread " << args->thread_id
198 // << ": Bucket selection failed - " << bucket_result.error_message
199 // << RESET << std::endl;
200 // return NULL;
201 // }
202
203 std::cout << GREEN << "Thread " << args->thread_id << " connected to bucket "
204 << args->bucket_name << RESET << std::endl;
205
206 // Perform operations - 10 times on default collection, 10 times on col1
207 // collection
208 for (int i = 0; i < 10; ++i) {
209 std::string base_key =
210 butil::string_printf("thread_%d_op_%d", args->thread_id, i);
211
212 // CRUD operations on default collection
213 perform_crud_operations_default(couchbase_ops, base_key, args->stats);
214
215 // CRUD operations on col1 collection
216 perform_crud_operations_col1(couchbase_ops, base_key, args->stats);
217
218 // Small delay between operations
219 bthread_usleep(10000); // 10ms
220 }
221
222 int successful = args->stats->operations_successful.load();
223 int attempted = args->stats->operations_attempted.load();
224 int failed = args->stats->operations_failed.load();
225
226 std::cout << GREEN << "Thread " << args->thread_id
227 << " completed: " << successful << "/" << attempted
228 << " operations successful, " << failed << " failed" << RESET

Callers

nothing calls this directly

Calls 6

string_printfFunction · 0.85
bthread_usleepFunction · 0.85
authenticateMethod · 0.80
loadMethod · 0.45

Tested by

no test coverage detected