Solved

using unix thr_create()

Posted on 1999-01-22
5
494 Views
Last Modified: 2010-05-18
this unix C++ program of mine use thr_create to have 2 thread function running....and the 2 thread function are friend of a particular CLASS(in order to use that class member)...can anyone tell me how should i declare the 2 thread function in the Class and also in the thr_create function. Thanks
0
Comment
Question by:kyh74
  • 3
  • 2
5 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
Comment Utility
I'm having trouble understanding you, but I think you are asking how to make the thread functions a member function of the class.  Is that right?  Well, they must be static member functions, which probably doesn't achieve your goal (I'm guessing your goal is to access a particular object of the class), for that they might as well be friend functions.    But anyways if you want the thread functions declare them inside the class, and preceed the declaration with the "static" keyword.  Now this makes the functions part of the class and they can access private emmbers of the class.  However, since they are static, they do not have a "this" parameter.  If they need to work with an object, they must be passed an object in the parameters (or use a globally declared object).   Often a thread function can be passed a void * parameter.  You can pass a pointer to an object in this parameter and cast it back to an object pointer.


Let me know if you have any questions.
0
 

Author Comment

by:kyh74
Comment Utility
sorry for the delay, i just came back from a holiday trip,

regarding the problem with the function thr_create(), this is the first time using this function and i don't really know how it work. can u offer me a simple example on how to create 2 thread(for example to print 2 message on screen) to work concurrently.  
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
I don't know the syntax for the thr_create() function or for the function it calls.  (These are not standard C++, so it depends on the OS, and i have no documentation for UNIX.)  But I will assume that thr_create takes two parameters, a pointer to a function that should be called as the start of the new thread and a void * pointer, that is not really used by thr_create, but that is passed  to the new thread function as its parameter.  (Windows tends to start threads this way)

class Message
{
   public:
      PrintMessage(char *Msg)
      {
         thr_create(ThreadFunction,Msg); // Start new thread to print message.
      };
   private:
      static void ThreadFunction(void *Param)
      {
         char *Msg = (char *) Param;  // Get message passed in parameters.
         cout << Msg << endl;
      };
};


int main()
{
    Message M;
   M.PrintMessage("this is printed from thread 2.");
   cout << This is printed from thrad 1." << endl;
   return 0;
}

Now technically that is dangerious to do, since both threads will be accessing cout, possibly at the same time.  This might cause the output to be garbled.  To be safe, mutexes or other synchronization objects must be used to insure that cout is not uses simultaneously by the two threads.

If this does not help and If you have documentation for thd_create(), can you post it?
0
 

Author Comment

by:kyh74
Comment Utility
Thanks for your help
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
you're welcome
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

763 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now