Pthread Read Text file

Source: Internet
Author: User

#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;}
    1. Read a text file, first count the number of lines of text;

    2. 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;

    3. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.