G
G
gibbonchik2016-10-31 15:18:29
C++ / C#
gibbonchik, 2016-10-31 15:18:29

How to call a method in C from a third-party thread that should be executed from the main thread?

Good day, friends!
I am writing a plugin for Pidgin. In it, I create a third-party thread, listen to the port there, and when a command is received, I send a message to the user. So in order to send a message you need to create create

PurpleConversation *conv = purple_conversation_new(PURPLE_CONV_TYPE_IM, acc, buddy);

but purple_conversation_new crashes the pidgin when executed from a third party thread. From the main thread everything is fine. This line creates a dialog box with the user. In the future, the message is sent to the user like this:
PurpleAccount *acc = FindNeededAccount(buddy);
if(acc == NULL) {
  printf("acc == NULL");
  return;
}
PurpleConversation *conv = purple_conversation_new(PURPLE_CONV_TYPE_IM, acc, buddy); 
purple_conv_im_send(PURPLE_CONV_IM(conv), message);

I usually write in C++ and there are TThread and std::thread which allow you to execute functions in sync with the main thread. How can this be done in pure C?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tsarevfs, 2016-10-31
@gibbonchik

In the thread that is listening to the port, we put the received data in a queue (not forgetting about blocking). In the main thread, we check the queue (again, with a lock) and if something new has arrived, we call the desired function. Most likely, the eventloop of libpurple itself is spinning in the main thread, so you will have to check, for example, by a timer .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question