CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Threaded View

  1. #1
    Join Date
    Oct 2008
    Posts
    45

    Unhappy Big Threading Problem

    I need to do merge sort with threading but it seems to take more time with threading rather than no threading , and I have core quad!

    please help

    Code:
    #include <iostream>
    #include <fstream>
    #include <windows.h>
    #include <process.h>
    #include <conio.h>
    #include <time.h>
    
    using namespace std;
    
    int threadCount=0;
    
    HANDLE ghMutex;
    
    class mSortArg
    {
    public:
    	int *a;
    	int start;
    	int end;
    	int threads;
    
    	mSortArg(int *A=NULL,int Start=0,int End=0,int Threads=0)
    	{
    		a=A;
    		start=Start;
    		end=End;
    		threads=Threads;
    	}
    };
    
    void merge(int a[],int s1,int e1,int s2,int e2)
    {
    	int s=(e1-s1+1)+(e2-s2+1);
    	int *b=new int [s];
    	int i1=s1;
    	int i2=s2;
    	int i=0;
    	while(i1<=e1 && i2<=e2)
    	{
    		if(a[i1]<a[i2])
    		{
    			b[i]=a[i1];
    			i1++;
    		}
    		else
    		{
    			b[i]=a[i2];
    			i2++;
    		}
    		i++;
    	}
    	for(int j=i1;j<=e1;j++)
    	{
    		b[i]=a[j];
    		i++;
    	}
    	for(int j=0;j<i;j++)
    	{
    		a[s1+j]=b[j];
    	}
    	delete []b;
    }
    
    void mergeSortHelp(int[],int,int,int);
    unsigned __stdcall threadFunc(void *param)
    {
    	mSortArg *arg=(mSortArg*)param;
    
    	mergeSortHelp(arg->a,arg->start,arg->end,arg->threads);
    
    	WaitForSingleObject(ghMutex,INFINITE);
    	threadCount--; //critical
    	ReleaseMutex(ghMutex);
    	_endthreadex(0);
    	return 0;
    }
    
    
    void mergeSortHelp(int a[],int start,int end,int threads)
    {
    	if(start>=end)
    		return;
    	int mid=start+(end-start)/2;
    	HANDLE th1=0;
    
    	mSortArg *arg=NULL;
    	WaitForSingleObject(ghMutex,INFINITE);
    	if(threadCount<threads) // comparison is critical
    	{
    		threadCount++; //critical
    		ReleaseMutex(ghMutex);
    
    		arg=new mSortArg(a,start,mid,threads);
    		th1=(HANDLE)_beginthreadex(NULL,0,threadFunc,arg,0,NULL);
    
    	}
    	else
    	{
    		ReleaseMutex(ghMutex);
    		mergeSortHelp(a,start,mid,threads);
    	}
    
    	mergeSortHelp(a,mid+1,end,threads);
    	WaitForSingleObject(th1,INFINITE);
    	delete arg;	//shallow delete
    	merge(a,start,mid,mid+1,end);
    }
    
    void mergeSort(int a[],int size,int threads)
    {
    	mergeSortHelp(a,0,size-1,threads);
    }
    
    int* mergeSort(char *fname,int threads,int &size)
    {
    	ifstream in(fname);
    	in>>size;
    	int *a=new int[size];
    	for(int i=0;i<size;i++)
    	{
    		in>>a[i];
    	}
    	in.close();
    	mergeSortHelp(a,0,size-1,threads);
    	return a;
    }
    
    bool check(int a[],int s)
    {
    	for(int i=0;i<s-1;i++)
    	{
    		if(a[i]>a[i+1]) return 0;
    	}
    	return 1;
    }
    int main()
    {
    	ghMutex=CreateMutex(NULL,FALSE,NULL);
    	ReleaseMutex(ghMutex); //just in case
    
    	int N=100;
    
    	clock_t t1=clock();
    	int a[100]={0};
    	for(int i=0;i<1000;i++)
    	{
    		mergeSort(a,N,0);// zero for no threading, put 1 for using only 1 thread, 2 for two threads ect...
    	}
    	clock_t t2=clock();
    	double t=(t2-t1)/(CLOCKS_PER_SEC*1000.0);
    
    	cerr<<t<<"s\n";
    	if(check(a,N)) cerr<<"OK!\n";
    	else cerr<<"oooops!!!\n";
    }

    any help is appreciated
    Last edited by compuKidd; January 18th, 2009 at 01:54 PM. Reason: following Lindley's advice {0}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured