Reputation: 6166
I have called a method using a secondary thread. From inside the method i need to call a method from main thread.
here is the structure
void main_thread_method()
{
}
void secondary_thread_method()
{
//do something here
call main_thread_method() here using main thread
}
pthread thread1;
pthread_create (&thread1, NULL, (void *) &secondary_thread_method, NULL);
pthread_join(thread1);
Upvotes: 4
Views: 5235
Reputation: 3089
I understood you want to invoke, from secondary thread, a method that must run in main thread. This is not possible. Invoked functions runs in the same thread. You mus use any kind of multi-threading communication method like semaphores, messages pooling, conditions, etc.
Upvotes: 1
Reputation: 24403
If your main thread is running a message pump you can post a message somehow to execute a function when your message is received.
Otherwise have a simple queue ( appropriate locking of course ). Add enough data into the queue so that main_thread_method can be called. (args etc). Periodically poll the simple queue for new messages in the main thread and process them.
Upvotes: 4