Click to See Complete Forum and Search --> : multithreading question


luvcloud
June 2nd, 2002, 09:17 PM
i was wondering is there a difference between multithreading in C++ and Visual C++? i'm new in C++, and i read that Visual C++ uses MFC, so under that, there are two types of threading supported by MFC - UI threads and Worker thread. How about C++, does it have its own way of handling threads and can that be used in Visual C++ as well?

Jupiter
June 2nd, 2002, 10:28 PM
Hi
There is no such difference between the threading in C++ and visual c++ except that the approach in vc++ is different but the whole concept of threading is the same. In vc++ there are classes available for threading where as in core c or c++ u ve to make use of run time libraries for the same. Now abt the funda of UI threads and worker threads is just a name given to threads depending on their use. There is nothing so special abt it. They are just normal threads as others.

A worker thread is commonly used to handle background tasks that the user shouldn't have to wait for to continue using your application. Tasks such as recalculation and background printing are good examples of worker threads

A user-interface thread is commonly used to handle user input and respond to user events independently of threads executing other portions of the application. The main application thread (provided in your CWinApp-derived class) is already created and started for you.


There are two overloaded versions of AfxBeginThread: one for user-interface threads and one for worker threads.

To begin execution of your worker thread, call AfxBeginThread providing the following information:

1] The address of the controlling function.
2] The parameter to be passed to the controlling function.
3] (Optional) The desired priority of the thread. The default is normal priority.
4] (Optional) The desired stack size for the thread. The default is the same size stack as the creating thread.
5] (Optional) CREATE_SUSPENDED if you want the thread to be created in a suspended state. The default is 0, or start the thread normally.
6] (Optional) The desired security attributes. The default is the same access as the parent thread.

To start your user-interface thread, call AfxBeginThread, providing the following information:

1] The RUNTIME_CLASS of the class you derived from CWinThread.
2] (Optional) The desired priority level. The default is normal priority.
3] (Optional) The desired stack size for the thread. The default is the same size stack as the creating thread.
4] (Optional) CREATE_SUSPENDED if you want the thread to be created in a suspended state. The default is 0, or start the thread normally.
5] (Optional) The desired security attributes. The default is the same access as the parent thread.

JMS
June 3rd, 2002, 03:11 PM
>> i was wondering is there a difference between multithreading
>> in C++ and Visual C++?

There is a difference. There is a difference in syntax and there is a difference in behavior as well as vocabulary.

In the Visual C++ world you've got your UI threads which use the messaging queue. You don't want more than one thread in a process blowing events into that straw or the the OS will caugh up a fur ball, thus Microsoft invented the UI thread.

Then you've got the Worker Thread, which is what Microsoft calls regular threads or threads which don't need to call the userinterface or messaging protocols.

Both of these thread conventions are non-standard. That's because threads in general are platform specific and heavily dependent upon the os, at least in C/C++.

In the Unix world threads come in two flavors too. But these flavors are usually exclusive to different platforms. In HP/UX (s is silent) threads are called user threads, not to be confused with Microsofts Userinterface threads. These threads basically deal with processor scheduling. On a multiplatform system user threads from the same process will not be scheduled on a different processor.

In the Solaris world they have what are called kernal threads. You guessed it, kernal threads can exist on different processors at the same time from the same process unlike user threads.

Now their are strengths and weeknesses to both approach. As a programmer a kernal thread can have thrashing problems and the memory is bing sync'ed across the different processors. A user thread doesn't have this syncing issue, but then a user thread can't really take advantage of a multi processor system, which is also a draw back.

I believe all MS theads are user threads, but I'm not positive about this.

Short answer to your question is yes thread are different across different platforms and syntax can differ on the same platform different compilers. Rogue waves makes good money smothing out such differences.