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

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

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]

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! (^ _ ^)

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);         
      }
   }
commented: Sweet!! +1

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!

>>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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.