I wanted to write the brute force algorithm for string matching by multithreading.
I have made the program by using a single thread.
In multithreading sample code, I have initialised the no. of threads to be strlen(text)-strlen(pattern).
I do the matching in the ThreadFunc.
Do I have to divide the text(A char array)into arrays of strlen(pattern)? If yes, Should i make a separate function for that?
How do I pass both the pattern and the divided text array in the beginthreadex() function?
Kindly help...

HANDLE   hth;
    HANDLE threadArray[100];
    long numberofthreads;
    unsigned  uiThread1ID;
    int x=10;
    cout<<"Enter the number of threads to be initialised\n";
    cin>>numberofthreads;
    for(int i=0; i<numberofthreads;i++)
    {
        hth = (HANDLE)_beginthreadex( NULL,         // security
                                   0,            // stack size
                                   ThreadFunc,
                                   &x,           // arg list
                                   CREATE_SUSPENDED,  // so we can later call ResumeThread()
                                   &uiThread1ID );

       if ( hth == 0 )
        printf("Failed to create thread 1\n");
        threadArray[i]= hth;
    }

Recommended Answers

All 5 Replies

Instead of passing 'x' pass in a struct, but make sure to cast it as necessary

// Multi_BruteForce.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<string>
#include <stdio.h>
#include<conio.h>
#include <windows.h>          // for HANDLE
#include <process.h>          // for _beginthread()
using namespace std;
static CRITICAL_SECTION m_CriticalSection;
static unsigned __stdcall ThreadFunc(void * threadObj){
    int *y= (int*)threadObj;
    EnterCriticalSection( &m_CriticalSection );
    //*y= *y.match();                           //I don't know what to compute here
    //int count=match((i-1)*((strlen(y)/numberofthreds)-1)+i, (i*((strlen(y)/numberofthreds-1))+i),*y,pattern);
    cout<<*y<<"\n";
    LeaveCriticalSection( &m_CriticalSection );
    return 1;
}
class Bruteforce{
    char* text;
    char* pattern;
    char*divided_text;
    int text_size;
    int pattern_size;
    //int count=0;
public:
    Bruteforce(void);
    ~Bruteforce(void);
    char* Text_read();
    char* Pattern_read();
    int match(int x, int y,char* text, char* pattern);

};

int match(int x,int y,char* text, char* pattern) //String matching
{

    int k=0,count=0;
    for(int j=x;j<=y;j++)
    {
        if(pattern[k]==text[j])
            k++;
        else break;
    }
    if(k==strlen(pattern))
        count++;
    return count;
}


char* Bruteforce::Text_read()       //reads the text from Text_Data.txt
{   
    FILE *f_text;
    f_text = fopen( "C:\\data\\Text_Data.txt" , "r");
    fseek(f_text, 0, SEEK_END);
   int text_file_size = ftell(f_text);

    fclose(f_text);
    f_text = fopen( "C:\\data\\Text_Data.txt" , "r");
    //char *text;
    text = new char[text_file_size+1];
    text[text_file_size]='\0';
                if(f_text != NULL)
                {
                    fread( text, sizeof(char), text_file_size,f_text);
                }

     fclose(f_text);
     return text;

}
char* Bruteforce::Pattern_read()            //reads the pattern from Pattern_Data.txt
{
     FILE *f_pattern;
    f_pattern = fopen ( "C:\\data\\Pattern_Data.txt" , "r");
    fseek (f_pattern,0, SEEK_END);
    int pattern_file_size = ftell (f_pattern);

    fclose(f_pattern);
    f_pattern = fopen ( "C:\\data\\Pattern_Data.txt" , "r");
    char *pattern;
    pattern = new char[pattern_file_size+1];
    pattern[pattern_file_size]='\0';


            if(f_pattern!= NULL)
                {
                    fread(pattern, sizeof(char),pattern_file_size ,f_pattern);
                }
    fclose(f_pattern);
    return pattern;

}
int main()
{   
    char* text;
    char* pattern;
    Bruteforce obj;
    text=obj.Text_read();
    pattern=obj.Pattern_read();
    InitializeCriticalSection(&m_CriticalSection);

    HANDLE   hth;
    HANDLE threadArray[100];


    unsigned  uiThread1ID;
    //int x;
    long numberofthreds=strlen(text)-strlen(pattern);
    for(int i=1;i<=numberofthreds;i++)           //Do I have to execute it in ThreadFunc?
    {
        match((i-1)*((strlen(text)/numberofthreds)-1)+i, (i*((strlen(text)/numberofthreds-1))+i),text,pattern);
    }

    for(int i=0; i<numberofthreds;i++)
    {
        hth = (HANDLE)_beginthreadex( NULL,         // security
                                   0,            // stack size
                                   ThreadFunc,
                                   &text,           // arg list      //I dont know what to pass here
                                   CREATE_SUSPENDED,  // so we can later call ResumeThread()
                                   &uiThread1ID );

       if ( hth == 0 )
        printf("Failed to create thread 1\n");
        threadArray[i]= hth;
    }

    DWORD   dwExitCode;
    for(long i =0; i<numberofthreds;i++)
    {
      ResumeThread(threadArray[i] );
    }
    WaitForMultipleObjects(numberofthreds,&threadArray[0],TRUE,INFINITE);
    for(long i =0; i<numberofthreds;i++)
    {
      GetExitCodeThread(threadArray[i], &dwExitCode );
      CloseHandle(threadArray[i]);
    }

    DeleteCriticalSection(&m_CriticalSection);
    getch();
    return 0;
}

Dear firstPerson, I am sorry but I couldn't understand what you meant by passing 'x' in a struct..Do i have to make it an object of class Bruteforce?
Also, since i've already defined char* text and char* pattern in the class Bruteforce, why do i have to define it again(like everytime i use match function, I have to pass it as argument)
This is my first multithreading program and I am completely confused.
Please help.

Since you're posting in a C++ thread, why don't you use C++ functions to work with? I'm just saying, it might help you.

Lucaci,
This works fine on visual C++, and I saw all these functions on the internet..Are they not in C++???
I don't know..I am just so confused.

they are usually C functions

#include <stdio.h>
#include<conio.h>

are C includes. C++ is derived from C, and MVC++ accepts as a compiler the language C, as most of compilers accepts both C and C++.
Here's a tutorial on I/O operations on files from C++: http://www.cplusplus.com/doc/tutorial/files/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.