[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Thread Management

Posted on 2006-04-06
8
Medium Priority
?
197 Views
Last Modified: 2013-12-03
Hi,

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

thanks,
0
Comment
Question by:arijit_rebaca
8 Comments
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16398412
Which platform ?
0
 

Author Comment

by:arijit_rebaca
ID: 16398416
in VC++
0
 

Author Comment

by:arijit_rebaca
ID: 16398419
windows
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16399501
0
 

Accepted Solution

by:
Rman01 earned 90 total points
ID: 16404353
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
 

Author Comment

by:arijit_rebaca
ID: 16414869
Is it possible without using any APIs

thanks,

0
 
LVL 11

Expert Comment

by:cup
ID: 16445441
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

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question