954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need help with threads in C++

Hi I need an example of how threads are coded and implemented in Dev-C++, any help would be greatly appreciated. :)

CodeBoy101
Junior Poster in Training
71 posts since Dec 2007
Reputation Points: 8
Solved Threads: 0
 

Try this and see if it works..... But the problem is some libraries does not support
(#include "XYLock.h")

#include <stdio.h>
#include  <iostream>
#include "XYLock.h"

XYCriticalSection myCriticalSection;

void ThreadProc(void *dummy)
{
    while(true)
    {
        try
        {
            XYLock myLock(&myCriticalSection);
            printf("Thread '%X' has the lock\n",::GetCurrentThreadId());
            throw "a test";
        }
        catch(char* p)
        {
            printf("Caught: %s\n",p);
        }
        ::Sleep(1000);
    }
}

void main()
{
    for(int i=0;i<100;i++)
    {
        if(_beginthread(ThreadProc,0,NULL)==-1)
        {
            printf("Failed to create a thread\n");
            return;
        }
    }

    while(true)
    {
        try
        {
            XYLock myLock(&myCriticalSection);
            printf("The main thread '%X' has the lock\n",::GetCurrentThreadId());
            throw "a test from the main thread";
        }
        catch(char* p) 
        {
            printf("Caught: %s\n",p); 
        } 
        ::Sleep(1000); 
    } 
}
Traicey
Posting Whiz in Training
283 posts since Mar 2008
Reputation Points: 26
Solved Threads: 19
 

Threads are not natively supported by the c++ language but there are several api functions and libraries that you can use. One of them is win32 api function CreateThread()

Also see this thread

[edit]^^^ Tracey's example is excellent too.[/edit]

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Thanks tracey, but I need help with a few things. First of all, I don't understand the code (I'm new a C++) so could you explain it to me? Second, what s the code supoosed to do? Thirdly, if I wanted to wait on an input from the user for a limited amount of ime, would I have to use threads and how could this be accomplished? Oh, one more thing my library doesn't have XYLock.h, so if I were to get a copy from you and link it to my library, would it work? Thank a lot! for your time! (^ _ ^)

CodeBoy101
Junior Poster in Training
71 posts since Dec 2007
Reputation Points: 8
Solved Threads: 0
 

As AncientDragon said, CreateThread() can be used in windows. Check it out on MSDN. Following is some code to show you how to use it. Note that the thread proc should be of the form DWORD WINAPI myThreadProc(void*someData) . someData is a pointer passed by CreateThread to your thread proc (I've set it to 0 in the following example).

#include <windows.h>
#include <iostream>

using namespace std;

DWORD WINAPI myThreadProc(void*someData){
   while(1){
      cout << "myThreadProc says hi" << endl;
      Sleep(1000);
      }
   }
   
main(){
   CreateThread(0, 0, myThreadProc, 0, 0, 0);

   for(int i = 0; i < 10; i++){
      cout << "main says hi" << endl;
      
      Sleep(1500);         
      }
   }
dougy83
Posting Whiz in Training
275 posts since Jun 2007
Reputation Points: 85
Solved Threads: 45
 

Hey doug, thanx for your suggestion, it complied and worked perfectly, but I don't really understand how the program works and I was wondering, how an example like this would work if an input from the user was required.

Correct me if I'm wrong, but based on the output that I'm getting, and looking at the code itself, it seems like the computer is switvching between tasks. Am I right?

(^ _ ^) Thanx!

CodeBoy101
Junior Poster in Training
71 posts since Dec 2007
Reputation Points: 8
Solved Threads: 0
 

>>it seems like the computer is switvching between tasks. Am I right?
Yes. Unless you have a duel processing computer then the os can only do one thing at the same exact time. It is just an illusion that it looks like two or more things run at the same time. The operating system contains a task switcher that gives only a small amount of CPU time to each program at a time. It has an array of threads that have to be serviced and switches the CPU between them in round-robin fashion. So when you call CreateThread you just add another thread to that list in the task scheduler.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Hey doug, thanks for the code, worked like a charm. Now I know how to use threads! Yeehaw!!

CodeBoy101
Junior Poster in Training
71 posts since Dec 2007
Reputation Points: 8
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You