Using threads in C ++

Can you tell me how I can use streams in C ++ programs and how to compile them, since they will be multithreaded? Can you tell me a good site where I can start as root?

thanks

+6
c ++ multithreading
source share
7 answers

I did not use it myself, but I was told that the Boost thread libraries make it incredibly lightweight.

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

+13
source share

For Unix / Linux / BSD there pthread library: tutorial .

I think the Win32 API has an equivalent.

+4
source share

I am using the tbb_thread class from the Intel Threading Prefabricated Block Library.

+3
source share

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

Sorry for the wait, but need to check :)

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

Well, that's why I talked with my professor, and he doesnโ€™t mind if I talk about it here. The following 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 streams and usage of this library:

0) This tutorial will explain the creation and use of threads on the Windows platform.

1) Themes in C ++ are usually encoded as part of the same source (unlike processes in which each process has its own source file and the main () function)

2) When a process is up and running, it can create other threads, creating the corresponding kernel calls.

3) Several threads are faster than several processes, since they are part of the same process, which leads to lower costs for the OS and reduced memory requirements.

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

5) (Make sure rt.h and rt.cpp are part of your โ€œsolutionโ€ and remember to include rt.h in the main.cpp file)

6) The following is part of the 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 } 

Arguments t1 in order: the name of the function acting as our stream, the status of the stream (it can be ACTIVE or SUSPECTED - depending on what you want) and, finally, a pointer to optional , which you can pass to the stream when creating. After executing some code, you want to call the WaitForThread () function.

7) The following is part of the code from your main main stream (in main.cpp, of course), where you will describe what the child stream does.

 UINT _ _stdcall ChildThread1(void *args) { . . . } 

The odd-looking thing is Microsoft's stream signature. I am sure that with some research you can understand how to do this in other OSs. An argument is additional data that can be passed to the child upon creation.

8) You can find detailed descriptions of member functions in the rt.cpp file. Here is a summary:

CThread () - the constructor responsible for creating the thread

Suspend () - pauses the child thread, effectively pausing it.

Resume () - Wakes a suspended child thread

SetPriority (int value) - changes the priority of the child stream to the value specified

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

TerminateThread () - terminates or kills a child thread

WaitForThread () - Pauses the parent thread until the child thread terminates. If the child thread has already completed, the parent does not pause

9) The following is an example of an example of a complete program. The smart thing you can do is create multiple instances of the same 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) What is it! The rt library includes a group of classes that allows you to work with processes and threads and other methods of parallel programming. Discover the rest;)

+1
source share

There are many C ++ compatible thread libraries. Therefore, you must first choose one. I prefer OpenMP or POSIX threads (also known as pthreads). How to compile it depends on the library you choose.

+1
source share

You can read my previous publication in SO format.

(Looking back, this post is a bit one-sided with respect to pthreads. But I am a Unix / Linux guy, and this approach seemed best with respect to the original topic.)

0
source share

Using threads in C / C ++:

 #include <iostream> using namespace std; extern "C" { #include <stdlib.h> #include <pthread.h> void *print_message_function( void *ptr ); } int main() { pthread_t thread1, thread2; char *message1 = "Thread 1"; char *message2 = "Thread 2"; int iret1, iret2; iret1 = pthread_create( &thread1, NULL, print_message_function (void*) message1); iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2); pthread_join( thread1, NULL); pthread_join( thread2, NULL); //printf("Thread 1 returns: %d\n",iret1); //printf("Thread 2 returns: %d\n",iret2); cout<<"Thread 1 returns: %d\n"<<iret1; cout<<"Thread 2 returns: %d\n"<<iret2; exit(0); } void *print_message_function( void *ptr ) { char *message; message = (char *) ptr; //printf("%s \n", message); cout<<"%s"<<message; } 
0
source share

All Articles