Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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.</p> <p>Sorry for the wait ahead, but gotta check :)</p> <p>++++++EDIT+++++++</p> <p>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</p> <p><a href="http://www.filefactory.com/file/7efbeb/n/rt_h" rel="nofollow noreferrer">http://www.filefactory.com/file/7efbeb/n/rt_h</a></p> <p><a href="http://www.filefactory.com/file/40d9a6/n/rt_cpp" rel="nofollow noreferrer">http://www.filefactory.com/file/40d9a6/n/rt_cpp</a></p> <p>Some points to be made about threads and the use of this library:</p> <p><strong>0) This tutorial will explain thread creation and use on a windows platform.</strong></p> <p>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() )</p> <p>2) When a process is up and running, it can create other threads by making appropriate Kernel calls.</p> <p>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.</p> <p>4) What you will be using in your case is the CThread class in the rt library.</p> <p>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)</p> <p>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.</p> <pre><code>void main() { CThread t1(ChildThread1, ACTIVE, NULL) ; . . . t1.WaitForThread() ; // if thread already dead, then proceed, otherwise wait } </code></pre> <p>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 <strong>optional</strong> data you may want to pass to the thread at creation. After you execute some code, you'll want to call the WaitForThread() function.</p> <p>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.</p> <pre><code>UINT _ _stdcall ChildThread1(void *args) { . . . } </code></pre> <p>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.</p> <p>8) You can find the detailed descriptions of the member functions in the rt.cpp file. Here are the summaries:</p> <p>CThread() - The constructor responsible for creating the thread</p> <p>Suspend() - Suspends a child thread effectively pausing it.</p> <p>Resume() - Wakes up a suspended child thread</p> <p>SetPriority(int value) - Changes the priority of a child thread to the value specified</p> <p>Post(int message) - Posts a message to a child thread</p> <p>TerminateThread() - Terminates or Kills a child thread </p> <p>WaitForThread() - Pauses the parent thread until a child thread terminates. If the child thread has already terminated, parent will not pause</p> <p>9) Below is an example of a sample complete program. A clever thing you can do is create multiple instantiations of a single thread.</p> <pre><code> #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 &lt; 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 &lt; 8; i ++) { printf ("Parent Thread: Creating Child Thread %d in Active State\n", i) ; Threads[i] = new CThread (ChildThread, ACTIVE, &amp;ThreadNum[i]) ; } // wait for threads to terminate, then delete thread objects we created above for( i = 0; i &lt; 8; i ++) { Threads[i]-&gt;WaitForThread() ; delete Threads[i] ; // delete the object created by ‘new’ } return 0 ; } </code></pre> <p>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 ;)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload