views:

146

answers:

3

I need to know which are the APIs/library used for multithreading by MSVC . If there are more than one , please let me know which is the most widely used.

If my question sounds too naive , its because I've never done threading before , and from my past experience , I know there are people here who can get me started/point me at the right direction , from which point I can start.

+4  A: 

Threading on Windows doesn't require any extra library, it's built right into the Win32 API. For example, to create a thread, call CreateThread. The complete list of threading functions can be found on MSDN at Process and Thread Functions.

Note that if you are writing a program that uses MSVCRT, you may want to call the _beginthread() family of functions instead. Doing so will help set up and tear down additional data structures used to support threading by the MSVCRT library.

Greg Hewgill
One more thing - the windows thread environment is preemptive , right ?
shan23
Yes, that's correct.
Greg Hewgill
Well then , can u take a look at http://stackoverflow.com/questions/1892104/can-a-thread-be-pre-empted-in-the-midst-of-a-system-call-to-kernel ? Can this scenario occur ( if not by default , made to occur ) on Windows ?
shan23
In Win7 x64 there is a new feature called User Mode Scheduled threads which will allow someone who's written a UMS Scheduler to receive notification when a blocking kernel call is made on a UMS thread and then schedule another UMS thread, this happens without a full context swap. ConcRT implements a UMS scheduler on Win7 x64, the 'event' how-to topic shows this.
Rick
+2  A: 

As @Greg said you can use CreateThread for creating a thread on windows . Other option is to use boost threads which IMHO provide a much better interface for handling them.

Naveen
And will also work on Linux and other platforms. And is almost identical to the threading model being added to C++0x. Definitely not a bad idea :)
jalf
+2  A: 

As others have suggested you can use CreateThread or _beginthread or the threadpool APIs, the process and threads reference is best for Win32 threading, you can also use boost::thread which is very close to the C++0x std::thread standard.

The other option if you're using Visual Studio is to take a look at the Parallel Pattern Library and Asynchronous Agents Library which are part of Microsoft's Concurrency Runtime (ConcRT) and are new in Visual Studio 2010. There are several how-to help topics which in the link that can help you get started here.

The API's in ConcRT are 'task' APIs rather than thread APIs and let you work at a slightly higher level of abstraction than threads. i.e. parallel loops, parallel pipelines and groups of tasks. Like boost::thread, the APIs are primarily setup to work with functors rather than the CreateThread / ThreadPool style APIs though there are APIs which are similar syntactically to CreateThread (Concurrency::Scheduler::ScheduleTask for example).

-Rick

Rick