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() ;
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;)