C++ linux共享库支持多线程应用程序回调
我需要创建一个共享库,它公开一组API,这些API将由多个进程使用,这些进程可能有多个调用API的线程 这个共享库反过来使用另一个第三方共享库,我需要向其注册回调。第三方库从不同的线程调用已注册的回调 我想知道如何在调用库中定义的API时阻止线程,并在从第三方库获得回调时释放它。此锁定不应阻止其他线程调用相同的API 我正在使用pthread库创建我的库 Psudo代码: 我的图书馆: 应用程序:C++ linux共享库支持多线程应用程序回调,c++,linux,multithreading,pthreads,shared-libraries,C++,Linux,Multithreading,Pthreads,Shared Libraries,我需要创建一个共享库,它公开一组API,这些API将由多个进程使用,这些进程可能有多个调用API的线程 这个共享库反过来使用另一个第三方共享库,我需要向其注册回调。第三方库从不同的线程调用已注册的回调 我想知道如何在调用库中定义的API时阻止线程,并在从第三方库获得回调时释放它。此锁定不应阻止其他线程调用相同的API 我正在使用pthread库创建我的库 Psudo代码: 我的图书馆: 应用程序: 我无法解决的确切问题是什么(如何)我需要设置锁定机制来阻止对我的库中定义的函数的调用,并等待来自第
我无法解决的确切问题是什么(如何)我需要设置锁定机制来阻止对我的库中定义的函数的调用,并等待来自第三方库的回调,前提是我的库应在多进程和多线程环境中使用。根据可用性和标准,您的最佳选择可能是使用Boost: Boost.threads用于多线程,Boost::interprocess用于进程间通信;看一看
对于多线程应用程序,通常会有某种消息队列(上面链接的底部)以线程安全的方式从任何线程获取消息,而无止境循环中的另一个线程则从该队列获取消息。这些消息通常包括一些命令、一些有效负载和一个指针,指向执行线程应该将结果放在哪里。这也可能是另一个boost::进程间通信结构。如果您可以使用C++11,您可能会对&friends感兴趣。您应该在函数API中创建一个条件变量,将其传递给回调函数,将回调函数注册到第三方库,并让API函数等待它。然后,您可以在回调结束时取消阻止它 伪代码:
void your_API_f()
{
define std::condition_variable;
pack it with your callback parameters
register the callback to 3rd party lib
invoke 3rd party func
wait on your condition variable
}
void your_callback(Parameters* p)
{
do whatever...
notify p->your_cond_variable you have finished
}
使用普通pthreads接口,可以使用条件变量:
int lib_init()
{
pthread_cond_init(&cond, NULL);
pthread_mutex_init(&mutex, NULL);
register_callback(callback);
}
int lib_deinit()
{
unregister_callback();
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
}
int callback(void *p)
{
pthread_mutex_lock(&mutex);
result[??] = p;
pthread_cond_broadcast(&cond);
pthread_mutex_unlock(&mutex);
}
int function1(int, int)
{
result[??] = NULL;
perform_action();
pthread_mutex_lock(&mutex);
while (result[??] == NULL)
pthread_cond_wait(&cond, &mutex);
pthread_mutex_unlock(&mutex);
}
int function2(char, char)
{
result[??] = NULL;
perform_action();
pthread_mutex_lock(&mutex);
while (result[??] == NULL)
pthread_cond_wait(&cond, &mutex);
pthread_mutex_unlock(&mutex);
}
pthread\u cond\u wait()
结果[??]
是某种方法的替代,您必须想出这种方法,将特定回调调用链接到与之相关的特定function1()
或function2()
调用。我无法使用Boost,因为我需要在其中部署库的环境(平台)。我只能使用pthread库&我需要编写支持库要求所需的所有内容。。。!多亏了Boost许可证,您可以简单地在应用程序中包含所需的所有Boost头文件和源文件。这和你自己写一样。Boost也是可移植的。谢谢@caf,我可以用您提供的示例代码开始我的实现。。。!
main()
{
init_lib();
....
create_new_thread();
....
function1(10, 20);
....
function2('c', 'd');
}
another_thread()
{
function2('a', 'b');
....
}
void your_API_f()
{
define std::condition_variable;
pack it with your callback parameters
register the callback to 3rd party lib
invoke 3rd party func
wait on your condition variable
}
void your_callback(Parameters* p)
{
do whatever...
notify p->your_cond_variable you have finished
}
int lib_init()
{
pthread_cond_init(&cond, NULL);
pthread_mutex_init(&mutex, NULL);
register_callback(callback);
}
int lib_deinit()
{
unregister_callback();
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
}
int callback(void *p)
{
pthread_mutex_lock(&mutex);
result[??] = p;
pthread_cond_broadcast(&cond);
pthread_mutex_unlock(&mutex);
}
int function1(int, int)
{
result[??] = NULL;
perform_action();
pthread_mutex_lock(&mutex);
while (result[??] == NULL)
pthread_cond_wait(&cond, &mutex);
pthread_mutex_unlock(&mutex);
}
int function2(char, char)
{
result[??] = NULL;
perform_action();
pthread_mutex_lock(&mutex);
while (result[??] == NULL)
pthread_cond_wait(&cond, &mutex);
pthread_mutex_unlock(&mutex);
}