Solved

using unix thr_create()

Posted on 1999-01-22
5
512 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

695 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