Thread Management

Hi,

I want to learn Thread Management in C++. Help me

thanks,
arijit_rebacaAsked:
Who is Participating?
 
Rman01Commented:
here is a little code that will help you understand the basics of threads
enjoy :)


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

#define SIZE 10


typedef struct thread
{
      char *str;
      int i;
      int j;
}thread;


int flag[2]={0};
int turn;


DWORD WINAPI print(LPVOID t);


DWORD WINAPI print(LPVOID t)
{
      thread *t1 = (struct thread *) t;

      int i;

      while (1)
      {      
            flag[t1->i]=1;
            turn=t1->j;
            while(flag[t1->j] && turn==t1->j);
      
            for(i=0; t1->str[i] != '\0';i++)
            {
                  cout<<t1->str[i];
                  cout.flush();
                  Sleep(10);
            }
            cout << endl;
            flag[t1->i]=false;
      }
      return 1;
}


void main()
{

      thread t1,t2;
      srand(time(NULL));
      t1.i=0;
      t1.j=1;
      t1.str=strdup("hello");
      t2.i=1;
      t2.j=0;
      t2.str=strdup("World");

      CreateThread(0,0,print,(void *)&t1,0,0);
      CreateThread(0,0,print,(void *)&t2,0,0);
      getch();
}
0
 
rajeev_devinCommented:
Which platform ?
0
 
arijit_rebacaAuthor Commented:
in VC++
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
arijit_rebacaAuthor Commented:
windows
0
 
rajeev_devinCommented:
0
 
arijit_rebacaAuthor Commented:
Is it possible without using any APIs

thanks,

0
 
cupCommented:
You have to use some form of basic API.  Try something like _beginthreadex.  That is about as low as you can get.

On MS, there is also something called fibers.  These are more like co-routines.  Strange to say the least.  I've never seen them being used commercially.  Great for impressing interviewers - most of them haven't heard of fibers either.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.