C++ File Writer-Reader application using Windows Threads - GeeksforGeeks

01 May, 2020
In this article, we will create a simple Writer-Reader application, which uses two threads, one for Writing into the file and another for Reading from the file. Here we will discuss the approach using Win32 Threads in C/C++. A windows thread can be created using the CreateThread() method.
Approach:
Create a Thread function for Reading data from the file// Thread function used to Read data from the fileDWORD WINAPI ReadFromAFile(PVOID lpParam){    // Create a buffer    char buffer[1024] = { 0 };      // Creating ifstream object    ifstream fileReader;      // Opening the file in read mode    fileReader.open("sample.txt");      // Reading the data into the buffer    cout << "Reading data from the file:";      // Printing the data onto the console    cout << buffer << endl;      // Closing the opened file    fileReader.close();      return 1;}Create a Thread function for Writing data into the file// Thread function used to Write data into the fileDWORD WINAPI WriteIntoAFile(PVOID lpParam){    // Create a buffer    char buffer[1024] = { 0 };      // Creating ofstream object    ofstream fileWriter;      // Opening the file in write mode    fileWriter.open("sample.txt");    cout << "Enter data to write into the file:";      // Write the given input into the file    fileWriter << buffer << endl;      // Closing the opened file    fileWriter.close();      return 1;}Create two threads using CreateThread function for both Writing and Reading data from the fileUse WaitForSingleObject to wait until the specified object is in the signaled state or time out interval elapses.
Create a Thread function for Reading data from the file// Thread function used to Read data from the fileDWORD WINAPI ReadFromAFile(PVOID lpParam){    // Create a buffer    char buffer[1024] = { 0 };      // Creating ifstream object    ifstream fileReader;      // Opening the file in read mode    fileReader.open("sample.txt");      // Reading the data into the buffer    cout << "Reading data from the file:";      // Printing the data onto the console    cout << buffer << endl;      // Closing the opened file    fileReader.close();      return 1;}
// Thread function used to Read data from the fileDWORD WINAPI ReadFromAFile(PVOID lpParam){    // Create a buffer    char buffer[1024] = { 0 };      // Creating ifstream object    ifstream fileReader;      // Opening the file in read mode    fileReader.open("sample.txt");      // Reading the data into the buffer    cout << "Reading data from the file:";      // Printing the data onto the console    cout << buffer << endl;      // Closing the opened file    fileReader.close();      return 1;}
Create a Thread function for Writing data into the file// Thread function used to Write data into the fileDWORD WINAPI WriteIntoAFile(PVOID lpParam){    // Create a buffer    char buffer[1024] = { 0 };      // Creating ofstream object    ofstream fileWriter;      // Opening the file in write mode    fileWriter.open("sample.txt");    cout << "Enter data to write into the file:";      // Write the given input into the file    fileWriter << buffer << endl;      // Closing the opened file    fileWriter.close();      return 1;}
// Thread function used to Write data into the fileDWORD WINAPI WriteIntoAFile(PVOID lpParam){    // Create a buffer    char buffer[1024] = { 0 };      // Creating ofstream object    ofstream fileWriter;      // Opening the file in write mode    fileWriter.open("sample.txt");    cout << "Enter data to write into the file:";      // Write the given input into the file    fileWriter << buffer << endl;      // Closing the opened file    fileWriter.close();      return 1;}
Create two threads using CreateThread function for both Writing and Reading data from the file
Use WaitForSingleObject to wait until the specified object is in the signaled state or time out interval elapses.
Below is the implementation of the above program:
// C++ program for File Writer-Reader// application using Windows Threads  #include <fstream>#include <iostream>#include <string.h>#include <winsock2.h>  using namespace std;  // Thread function used to Read data from the fileDWORD WINAPI ReadFromAFile(PVOID lpParam){    // Create a buffer    char buffer[1024] = { 0 };      // Creating ifstream object    ifstream fileReader;      // Opening the file in read mode    fileReader.open("sample.txt");      // Reading the data into the buffer    cout << "Reading data from the file:" << endl;      fileReader >> buffer;    // Printing the data onto the console    cout << buffer << endl;      // Closing the opened file    fileReader.close();    return 1;}  // Thread function used to Write data into the fileDWORD WINAPI WriteIntoAFile(PVOID lpParam){    // Create a buffer    char buffer[1024] = { 0 };      // Creating ofstream object    ofstream fileWriter;      // Opening the file in write mode    fileWriter.open("sample.txt");    cout << "Enter data to write "         << "into the file:"         << endl;    cin >> buffer;      // Write the given input into the file    fileWriter << buffer << endl;      // Closing the opened file    fileWriter.close();      return 1;}  // Driver codeint main(){    WSADATA WSAData;    char buffer[1024];    DWORD tid;    ofstream fileWriter;    ifstream fileReader;    HANDLE t1, t2;    int choice, flag = 1;      while (flag) {        cout << "================================"             << "========================"             << "==================" << endl;        cout << "Select your option"             << "\t1.Run the application "             << "\t2.Exit" << endl;        cin >> choice;          switch (choice) {          case 1:            // Create the first thread for Writing            t1 = CreateThread(NULL, 0,                              WriteIntoAFile,                              &fileWriter,                              0, &tid);            WaitForSingleObject(t1, INFINITE);              // Create the second thread for Reading            t2 = CreateThread(NULL, 0,                              ReadFromAFile,                              &fileReader,                              0, &tid);            WaitForSingleObject(t2, INFINITE);            break;          case 2:            // Exiting the application            cout << "Thank you for using"                 << " the application"                 << endl;            flag = 0;            break;          default:            // For any query other than 1 and 2            cout << "Enter a valid query!!"                 << endl;        }    }    return 0;}
Run the application in the cmd using the command:
g++ MultiThreading.cpp -lws2_32
Output:
cpp-multithreading
Articles
C++ Programs
Operating Systems
Operating Systems
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Time Complexity and Space Complexity
Docker - COPY Instruction
SQL | Date functions
Time complexities of different data structures
Difference between Class and Object
Header files in C/C++ and its uses
How to return multiple values from a function in C or C++?
C++ Program for QuickSort
Program to print ASCII Value of a character
CSV file management using C++