Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

chidambaram

macrumors member
Original poster
Mar 9, 2008
67
0
Hi all,

I am working in carbon c++ application using XCode in MAC Machine.

I have to create two new threads in my program.

How can i create a thread and use it in my program.

Please give some sample samples.

Thanks in advance and urgent plz.........
 

chidambaram

macrumors member
Original poster
Mar 9, 2008
67
0
Thanks for ur reply..

Is it possible to place function PosixThreadMainRoutine (which in given in the program of your link) in a class.

If i use that function as member function then following errors will occur

1. ISO c++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say '&MainWindow::posixThreadMainRoutine'

2. Cannot convert 'void*(MainWindow::*)(void*)' to 'void*(*)(void*)' for argument 3 to int pthread_create(_opaque-pthread_t**,const pthread_attr_t*,void*(*)(void*),void*)'

In my class i declared the function as

void * PosixThreadMainRoutine(void*)

I called that function as

void MainWindow::posixThreadMainRoutine(void *data)
{
printf("This is function");

}
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
I don't think you can have it call directly into a C++ function, but the callback function can take a pointer to some user data, which you set in the last argument of pthread_create(). I don't have an example but you need to set the argument to "this" and then reference that inside the thread.
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
Thanks for ur reply..

Is it possible to place function PosixThreadMainRoutine (which in given in the program of your link) in a class.

If i use that function as member function then following errors will occur

1. ISO c++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say '&MainWindow::posixThreadMainRoutine'

Find a good tutorial about pthreads. There are plenty on the web. Most explain how to do what you want to do. Here's what you do: Write your class. In the implementation of the class, write a static function (not a class member, a plain static function) posix_thread_func that has the form that you need for the posix thread. In some class member function you will create the thread; that member function will create the posix thread and pass posix_thread_func and its own "this" pointer to the thread creation. As a result, posix_thread_func will be called with "this" as its argument. Cast the argument to the proper class pointer, and you are there.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.