Thread Management

Hi,

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

thanks,
arijit_rebacaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rajeev_devinCommented:
Which platform ?
arijit_rebacaAuthor Commented:
in VC++
arijit_rebacaAuthor Commented:
windows
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

rajeev_devinCommented:
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();
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
arijit_rebacaAuthor Commented:
Is it possible without using any APIs

thanks,

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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Fonts Typography

From novice to tech pro — start learning today.