Solved

using unix thr_create()

Posted on 1999-01-22
5
497 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
ID: 1184909
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
ID: 1184910
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
ID: 1184911
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
ID: 1184912
Thanks for your help
0
 
LVL 22

Expert Comment

by:nietod
ID: 1184913
you're welcome
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Handling string inputs in C/Linux 23 179
Load and store *.pnm image file 1 75
Move constructor only called if marked noexcept? 6 98
C++ mouse_event mouse look 7 68
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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 viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

911 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

19 Experts available now in Live!

Get 1:1 Help Now