views:

1302

answers:

7

Hi friends.

Can you tell me how can I use threads in C++ programs, and how can I compile it as it will be multithreaded? Can you tell me some good site where I can start from root?

Thanks

+4  A: 

For Unix/Linux/BSD, there's pthread library: tutorial.

I guess there are equivalent in Win32 API.

eed3si9n
There is pthreads-win32: http://sourceware.org/pthreads-win32/
CesarB
+10  A: 

I haven't used it myself, but I'm told that the Boost thread libraries make it incredibly easy.

http://www.boost.org/doc/libs/1_37_0/doc/html/thread.html

Smashery
They make it VERY easy. :)
Rob
Threads are never easy, regardless of the library/API
Nemanja Trifunovic
A: 

Hi,

(for c/c++) : http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1875.html

Check the threading tutorial at msdn (for c#)

http://msdn.microsoft.com/en-us/library/aa645740(VS.71).aspx

Samiksha
A: 

You may want to read my earlier posting on SO.

(In hindsight, that posting is a little one-sided towards pthreads. But I'm a Unix/Linux kind of guy. And that approach seemed best with respect to the original topic.)

Mr.Ree
+1  A: 

I use a library my university prof wrote. It is very simple to implement and works really well (used it for quite some time now). I will ask his permission to share it with you.

Sorry for the wait ahead, but gotta check :)

++++++EDIT+++++++

Ok, so I talked to my prof and he doesn't mind if I share it here. Below are the .h and .cpp files for the 'RT Library' written by Paul Davies

http://www.filefactory.com/file/7efbeb/n/rt_h

http://www.filefactory.com/file/40d9a6/n/rt_cpp

Some points to be made about threads and the use of this library:

0) This tutorial will explain thread creation and use on a windows platform.

1) Threads in c++ are usually coded as part of the same source (unlike processes where each process has its own source file and function main() )

2) When a process is up and running, it can create other threads by making appropriate Kernel calls.

3) Multiple threads run faster than multiple processes since they are a part of the same process which results in less of an overhead for the OS, and reduced memory requirements.

4) What you will be using in your case is the CThread class in the rt library.

5) (Make sure rt.h and rt.cpp are a part of your 'solution' and make sure to include rt.h in your main.cpp)

6) Below is a part of code from your future main thread (in main.cpp, of course) where you will create the thread using the CThread class.

void main() 
{
    CThread   t1(ChildThread1, ACTIVE, NULL) ;  
    . . . 
    t1.WaitForThread() ;                // if thread already dead, then proceed, otherwise wait

}

The arguments of t1 in order are: Name of the function acting as our thread, the thread status (it can be either ACTIVE or SUSPENDED - depending on what you want), and last, a pointer to an optional data you may want to pass to the thread at creation. After you execute some code, you'll want to call the WaitForThread() function.

7) Below is a part of code from your future main thread (in main.cpp, of course) where you will describe what the child thread does.

UINT  _ _stdcall  ChildThread1(void *args)   
{

    . . .   
}

The odd looking thing there is Microsoft's thread signature. I'm sure with a bit of research you can figure out how to do this in other OSs. The argument is the optional data that could be passed to the child at creation.

8) You can find the detailed descriptions of the member functions in the rt.cpp file. Here are the summaries:

CThread() - The constructor responsible for creating the thread

Suspend() - Suspends a child thread effectively pausing it.

Resume() - Wakes up a suspended child thread

SetPriority(int value) - Changes the priority of a child thread to the value specified

Post(int message) - Posts a message to a child thread

TerminateThread() - Terminates or Kills a child thread

WaitForThread() - Pauses the parent thread until a child thread terminates. If the child thread has already terminated, parent will not pause

9) Below is an example of a sample complete program. A clever thing you can do is create multiple instantiations of a single thread.

    #include “..\wherever\it\is\rt.h” //notice the windows notation

    int       ThreadNum[8] = {0,1,2,3,4,5,6,7} ;   // an array of thread numbers


    UINT _ _stdcall ChildThread (void *args)    // A thread function 
    {   
        MyThreadNumber = *(int *)(args);  

        for ( int i = 0; i < 100; i ++)
         printf( "I am the Child thread: My thread number is [%d] \n", MyThreadNumber) ;

        return 0 ;
    }
int     main()
{
    CThread  *Threads[8] ; 

// Create 8 instances of the above thread code and let each thread know which number it is.


    for ( int i = 0; i < 8; i ++) {
     printf ("Parent Thread: Creating Child Thread %d in Active State\n", i) ;
     Threads[i] = new CThread (ChildThread, ACTIVE, &ThreadNum[i]) ;
    }

    // wait for threads to terminate, then delete thread objects we created above

    for( i = 0; i < 8; i ++) {
     Threads[i]->WaitForThread() ;
    delete Threads[i] ; // delete the object created by ‘new’
    }
    return 0 ;
}

10) That's it! The rt library includes a bunch of classes that enables you to work with processes and threads and other concurrent programming techniques. Discover the rest ;)

caglarozdag
+3  A: 

I use tbb_thread class from intel threading building blocks library.

Lazin
I also use tbb, good stuff
Robert Gould
+1  A: 

There are many threads libraries wich are compatible with c++. So at first you must select one. I prefer OpenMP or POSIX threads (also known as pthreads). How to compile it depends on library you have choose.

Pawka