efficient use of multiple threads

Jul 4, 2011 at 2:42pm
closed account (zwA4jE8b)
I was having too much trouble with CreateDialog() function in windows so I just decided that my help messagebox would be the product of a new thread.

Is this an efficient use of multi-threading?

The program now works as intended. The main windows continues to run while the help window is displayed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
       case 0x48:
	_beginthread(helpfunc, 0, NULL);
	break;
           }
         return 0;
       } break;
    }
     return DefWindowProcA(hWnd, message, wParam, lParam);
 }

void __cdecl helpfunc( void *arg )
{
	MessageBoxA(NULL, "NUMPAD + : Increase speed\n"
					 "NUMPAD - : Decrease speed\n"
					 "NUMPAD 8 : Increase amplitude\n"
					 "NUMPAD 2 : Decrease amplitude\n"
					 "NUMPAD 4 : Increase frequency\n"
					 "NUMPAD 6 : Decrease frequency\n"
					 "Enter    : Draw a small circle\n"
					 "\nChanges are small, hold the button",
					 "Help", MB_OK);
}


I did not us _beginthreadEX() because I did not see a purpose for the extra params (except to disallow multiple help windows, but I am not yet worried about that)
Last edited on Jul 4, 2011 at 2:54pm
Jul 4, 2011 at 3:26pm
closed account (S6k9GNh0)
Well, whether it's efficient or not may not matter. It's simply easier to use threads when you want two things executed at the same time.

But in reality, is MessageBoxA a blocking function? In a GUI where efficiency may not be #1, it probably doesn't matter whether it's multi-threaded or not. If it simplifies the code then sure. I've experienced developers throw up a thread pool and then toss all functions onto the thread pool and let whatever thread handling system they have implemented choose which thread to use the given function on. This can be efficient with computers with more than one core apparently. std::bind and Boost.Bind also simplify this task greatly. However, thread safety comes to mind when doing such a method and the complicated workarounds needed.
Last edited on Jul 4, 2011 at 3:29pm
Jul 4, 2011 at 3:33pm
closed account (zwA4jE8b)
MessageBoxA when placed in line 2 halts the execution of the main window and has to exit before it resumes. So I decided to just make a new thread.


the old code is posted here...
http://cplusplus.com/forum/general/45994/
Jul 4, 2011 at 3:39pm
closed account (zwA4jE8b)
also I was wondering how to only allow 1 instance of the thread to be created at a time.

so far I have...
1
2
3
4
5
case 0x48:
	static uintptr_t t = 0;
              if (!t)
	        t = _beginthread(helpfunc, 0, NULL);
	break;


If I pass &t as the parameter to my helpfunc, is there a way to set t = 0 when MB_OK is pressed?
Last edited on Jul 4, 2011 at 3:39pm
Jul 4, 2011 at 3:54pm
closed account (S6k9GNh0)
static variables are dangerous in the idea they are never unallocated (generally anyways).

http://stackoverflow.com/questions/331536/windows-threading-beginthread-vs-beginthreadex-vs-createthread-c

http://msdn.microsoft.com/en-us/library/kdzttdcb%28v=vs.80%29.aspx

Be sure and at least read the excerpt from Microsoft on how to handle the return value.
Topic archived. No new replies allowed.