Link to home
Start Free TrialLog in
Avatar of jonnykeogh
jonnykeogh

asked on

Threads

Hi,

I'm having a bit of trouble with this whole threads thing, how do I get them to work?
Sample code? Tutorial web sites? Any thing that will help me!

Thnx, it's appreciated!

Jonny K
Avatar of Member_2_1001466
Member_2_1001466

You have some good suggestions above.  Here is some sample code for Windows that hopefully will clear things up, along with an explanation of what it is doing.

Basically this program has one thread updating values in a global array and the main thread outputting the contents of the array.  If the main thread interrupts the worker thread before it has finished updating *all* of the elements in the array, then the main thread will output values that are not all the same (obviously).  
However the use of the CS ensures that the worker thread finishes updating all of the elements in the array *before* the main thread can output them.

Although it uses beginthread, you can easily modify it to use beginthreadex.  Just look up the parameters on MSDN.

#include <windows.h>
#include <process.h>
#include <stdio.h>

CRITICAL_SECTION cs;

int a[5];

void Thread(void* pParams)
{
     int i, num = 0;
     
    while (1) {
          EnterCriticalSection( &cs );
          printf("In the thread process, num is %i\n", num);
          for (i = 0; i < 5; i++) {
               a[i] = num;
          }
          num++;
          LeaveCriticalSection( &cs );
         
    }
}

int main(void)
{
     InitializeCriticalSection( &cs );

     _beginthread(Thread, 0, NULL);
     while(1) {
          EnterCriticalSection( &cs );
          printf("In the main process\n");
        printf("%d %d %d %d %d\n",  a[0], a[1], a[2], a[3], a[4] );
        LeaveCriticalSection( &cs );
     }
     return 0;
}

Hope this helps.
Avatar of jonnykeogh

ASKER

I'll just take the time now to point out that im using Borland C++ 5.0. Sorry for being a pain if I have.
Oh and also, I'm only a beginner at the whole C++ thing as well- havent used it in ages and don't know much advanced stuff.
In BCB it is creating a TThread object instead of CWinThread. Your code goes into Execute method. Instead of the use of critical sections you can call functions from the main thread using Synchronize (function). Synchronization is done internally inside VCL.
Perhaps you can ask moderators (in Community support) to move this Q to "C++ builder" Topic Area (TA) to get all Builder experts aware of this Q. Or post a Q worth 20 points in there linking to this Q. That link Q you should be able to delete as long as no one post a comment to it.
!!Major problem- it brings up 'Undefined function _beginthread'!!
Messages I get from the 'Messages' dialog box:

Info :Compiling E:\Documents and Settings\Jonathon Keogh\Desktop\Threads\threadtest.cpp
Warn :threadtest.cpp(24,2):Parameter 'pParams' is never used
Error:threadtest.cpp(30,19):Call to undefined function '_beginthread'
Warn :threadtest.cpp(37,12):Unreachable code

^^ Messages I get ^^
Error:threadtest.cpp(30,19):Call to undefined function '_beginthread'
^^ That bits got a red exclamation mark ^^

Warn has a yellow excalamation mark.

Info has no exclamation mark.
In main.cpp you need the following

   FThread = new TMyThread (true);
   FThread->Priority = tpLower;
   FThread->Resume();

in TMyThread.cpp (class TMyThread : public TThread in header)

TMyThread::Execute ()
{
   while (!Terminated) {
        // here comes your code
   }
}
OK, totally confused, could you create some sample code that will create a couple of threads and show me some interaction coming from them.

Thnx,
Jonny
Can you be a bit more specific? IMHO you create a thread when you can seperate its work from the main thread. There should be as little interaction with the main thread or even between threads. Interaction raises the need for proper synchronization which adds even more complexity to an application than threads alone would do. Unproblematic interaction is sending message to and from the thread. There are good articles on messages on flounder:
http://www.flounder.com/mvp_tips.htm#Habits%20Highly%20Defective
and messages can be used in BCB as in VC++.
Could you make some code that shows text printing away in the background whilst a message box is open? or something

Thnx,
Jonny K
ASKER CERTIFIED SOLUTION
Avatar of Member_2_1001466
Member_2_1001466

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial