#include <pthread.h> #include <fstream> #include <iostream> #include < string> #include <vector>using namespace std; #define LINE_PER_THREAD 1024pthread_mutex_t count_mutex = pthread_mutex_initializer;struct param{ vector<string> vstr; int offset; int tidx;};/ /kernel functionvoid * std_output (VOID&NBSP;*PA) { struct param *local = (struct param *) pa; vector<string> vstr = local->vstr; int offset = local->offset; int tidx = local->tidx; pthread_mutex_lock (& Count_mutex); int count = 0; int i = offset; while (I < vstr.size () && i < offset + line_ Per_thread) { count++; i++; } cout << " thread " << tidx << has << count << Lines " << endl; pthread_mutex_unlock (&count_mutex);} Int main () { ifstream input_data ("input.txt"); int line_num = 0; string line; vector<string > vstring; while (Input_data.is_open ()) { if (!input_data.eof ()) { Getline (Input_data, line); vstring.push_back (line); line_num++; } else{ cout << line_num << endl; break; } } input_data.close (); int thread_num = (line_num + line_per_thread - 1) / LINE_PER_THREAD; pthread_t threads[thread_num]; for (int i = 0; i < thread_num; i++) { struct param *str_ Param = new param (); str_param->vstr = vstring; str_param->offset = i * LINE_PER_THREAD; str_param->tidx = i; pthread_create (&threads[i], NULL, std_output, (void *) str_param); } for (int i = 0; i < thread_num; i++) pthread_join (threads[i], null); return 0;}
Read a text file, first count the number of lines of text;
By 1024 lines per line, calculate how many threads are required; When calculating, you can use the formula (n + 1023)/1024, where n is the total row number of the text file;
Std_output is a kernel function that uses a mutex lock, which determines which part of the data to process based on offset offsets. The program simply prints out how many rows are contained in a certain section.
Output:
18207
Thread 0 has 1024x768 lines
Thread 1 has 1024x768 lines
Thread 2 has 1024x768 lines
Thread 3 has 1024x768 lines
Thread 4 has 1024x768 lines
Thread 5 has 1024x768 lines
Thread 6 has 1024x768 lines
Thread 7 has 1024x768 lines
Thread 8 has 1024x768 lines
Thread 9 has 1024x768 lines
Thread Ten has 1024x768 lines
Thread one has 1024x768 lines
Thread has 1024x768 lines
Thread has 1024x768 lines
Thread + has 1024x768 lines
Thread has 1024x768 lines
Thread + has 1024x768 lines
Thread # has 799 lines
This article from "Hu A Knife" blog, declined reprint!
Pthread Read text file