C++ File Writer-Reader application using Windows Threads

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:

  1. Create a Thread function for Reading data from the file
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Thread function used to Read data from the file
    DWORD 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;
    }

    chevron_right

    
    

  2. Create a Thread function for Writing data into the file
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Thread function used to Write data into the file
    DWORD 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;
    }

    chevron_right

    
    

  3. Create two threads using CreateThread function for both Writing and Reading data from the file
  4. 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 file
DWORD 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 file
DWORD 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 code
int 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;
}

chevron_right


Run the application in the cmd using the command:

g++ MultiThreading.cpp -lws2_32

Output:

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.