View Full Version : how to create a new thread using carbon c++?
chidambaram
Apr 4, 2008, 12:09 AM
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.........
kainjow
Apr 4, 2008, 12:39 AM
You want to use POSIX threads: http://developer.apple.com/documentation/Cocoa/Conceptual/Multithreading/CreatingThreads/chapter_4_section_3.html#//apple_ref/doc/uid/10000057i-CH15-SW12
chidambaram
Apr 4, 2008, 07:41 AM
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
Apr 4, 2008, 08:21 AM
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
Apr 4, 2008, 09:11 AM
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.
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.