Solved

using unix thr_create()

Posted on 1999-01-22
5
506 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Quicksort a dynamic deque 33 79
C++ error cannot convert from std::string to const char * 6 308
White board coding practice 3 95
How do i run a c++ file? 15 52
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…
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 difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

726 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