CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2008
    Posts
    84

    [RESOLVED] C++ Multi-threading

    What is a good way to show the efficiency of multi-threading in C++?

    I need to show how much multi-threading can help vs single-threaded applications, and here is what I am currently using:

    Code:
    #include <stdio.h>
    #include <process.h>
    #include <windows.h>
    #include <ctime>
    #include <iostream>
    
    using namespace std;
    
    float w = 21;
    float x = 21;
    BOOL thread1 = TRUE;
    BOOL thread2 = TRUE;
    
    void Thread1( void* pParams)
    {
    	for (int i = 0; i < 100000000; i++)
    	{
    		w = w * 3;
    	}
    	thread1 = FALSE;
    }
    
    void Thread2( void* pParams)
    {
    	for (int j = 0; j < 100000000; j++)
    	{
    		x = x * 3;
    	}
    	thread2 = FALSE;
    }
    
    int main(void)
    {
    	clock_t start = clock();
    
    	_beginthread( Thread1, 0, NULL);
    	_beginthread( Thread2, 0, NULL);
    	while ( thread1 && thread2 )
    	{
    		Sleep( 100L );
    	}
    	cout << ((clock() - start ) / (double)CLOCKS_PER_SEC ) << '\n';
    	
    	printf("%d %d\n", w,x);
    
    	w = 21;
    	x = 21;
    
    	clock_t start2 = clock();
    	Thread1(0);
    	Thread2(0);
    	cout << ((clock() - start2 ) / (double)CLOCKS_PER_SEC ) << '\n';
    	printf("%d %d\n", w,x);
    
       return 0;
    }
    Now, I know using the timer method I used is not the most accurate, but it gives fairly average times repeatedly. My main questions are, with multi-threading this calculation the multi-threaded variant takes roughly 10 seconds to finish on my system, the single-threaded variant (lower part of the code) takes roughly 15 seconds.

    Now, I know I cant expect the speeds to double going from single-threaded to multi-threaded, but with simple pure calculations, should I see more than a 50% increase in speed? I have a quad-core and if I add in a third calculation, using multi-core (now 3-cores going at once) I get ~17 seconds, whereas doing those same calculations but single-threaded takes only about 23 seconds.......why does the speed increase drop to only about 35%?

    I'm sure my code is nowhere near perfect for showing how much more efficient multi-core is in handling multiple calculations, but why does the efficiency drop nearly 15% when I add in a third calculation? I would think the speed of the calculation would stay roughly the same for the multi-threaded variant because all cores are processing at roughly the same speeds, I did not expect it to take ~70% longer from adding in the third core.

    That being said, any answers as to why this program is acting this way, and any major improvements (such as more accurate timers or more efficient code) will be awesome. I'm not asking for help in making it more efficient, but any improvements pointed out to me will help me better understand C++ and its multi-threading.

  2. #2
    Join Date
    Jan 2009
    Posts
    1,689

    Re: C++ Multi-threading

    I have a feeling it has to do with your call to Sleep. Sleep stops the process. I don't know how to use the windows API, but pthreads has a function called pthread_join. Join the thread and wait for them to finish that way, that should show you a better speed increase.

  3. #3
    Join Date
    May 2008
    Posts
    84

    Re: C++ Multi-threading

    Quote Originally Posted by ninja9578 View Post
    I have a feeling it has to do with your call to Sleep. Sleep stops the process. I don't know how to use the windows API, but pthreads has a function called pthread_join. Join the thread and wait for them to finish that way, that should show you a better speed increase.
    Ok, thinking about what you mentioned led me to this post: http://www.codeguru.com/forum/showthread.php?t=289677

    Implementing that with four threads I get the following results:

    ~ 21.2 seconds - Four threads/four calculations
    ~ 31.35 seconds - One thread/four calculations
    ~ 47.8% increase

    ~ 17.5 seconds - Three threads/three calculations
    ~ 23.5 seconds - One thread/three calculations
    ~ 34% increase

    ~ 11.37 seconds - Two threads/two calculations
    ~ 15.67 seconds - One thread/two calculations
    ~ 37.8% increase

    Roughly the same with a single thread/calculation as expected.

    So oddly enough, it lowered my efficiency increase with two threads (down from ~50%), still had the slight drop in efficiency to three threads, and kept my 4 thread efficiency around the same (didn't mention it before).

    But at the same time, I'm not slamming another core with a loop thats doing nothing but telling the process to sleep another 100 MS.

    Here's my updated main function:

    Code:
    int main(void)
    {
    	clock_t start = clock();
    
    	HANDLE hThread;
    	HANDLE hThread2;
    	HANDLE hThread3;
    	HANDLE hThread4;
    
    	hThread = (HANDLE)_beginthread(Thread1, 0, NULL);
    	hThread2 = (HANDLE)_beginthread(Thread2, 0, NULL);
    	hThread3 = (HANDLE)_beginthread(Thread3, 0, NULL);
    	hThread4 = (HANDLE)_beginthread(Thread4, 0, NULL);
    
    	WaitForSingleObject(hThread, INFINITE);
    	WaitForSingleObject(hThread2, INFINITE);
    	WaitForSingleObject(hThread3, INFINITE);
    	WaitForSingleObject(hThread4, INFINITE);
    
    	CloseHandle(hThread);
    	CloseHandle(hThread2);
    	CloseHandle(hThread3);
    	CloseHandle(hThread4);
    
    	cout << ((clock() - start ) / (double)CLOCKS_PER_SEC ) << '\n';
    
    	w = 21;
    	x = 21;
    	y = 21;
    	z = 21;
    
    	clock_t start2 = clock();
    	Thread1(0);
    	Thread2(0);
    	Thread3(0);
    	Thread4(0);
    	cout << ((clock() - start2 ) / (double)CLOCKS_PER_SEC ) << '\n';
    
       return 0;
    }
    The rest is the same, except I added duplicate functions for thread3 and thread4.....exactly the same as 1 and two, but new variables so they dont interfere with each other.

    No improved efficiency from what I can tell, but its understandable that waiting for a thread to finish (if need be) is better than looping until the function it is running is finished.

    Any more thoughts on how to improve it will be helpful.

  4. #4
    Join Date
    May 2008
    Posts
    84

    Re: C++ Multi-threading

    Ok, got the "OK" to do this in C# instead, much easier to multi-thread in C# IMO, have it showing practically perfect efficiency.....1 thread takes ~1.2 seconds, two threads takes nearly the same time (whereas doing the same calculations as two threads, but only on one thread takes roughly twice the amount of time). Much happier with the result as a whole. Thanks for pointing out waiting for a thread though (leading me to finding out how to do so in C++).

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