Hi all, I'm developing a library. I would like that this library could make differents callbacks to the user part. Untill now, I had the simplest way to do it, I had stored a pointer to the user class and I was making calls when something happened, but with this implementation, if the user makes a while true in his implementation body of the callback, it will be block all my library.
Later I thought that with threads, I'll solved this problem in a little few time but if I run a thread and I have to wait for the join, the user could block me again with the same while true.
Later thought that a pool of threads could be my solution, but if the user makes the same while true, the worker thread will be waiting for the end of the thread, and all callbacks will be enqueue and never dequeue (correct me if I am wrong).

The only solution that could fits me is run the callback but without waiting for the join. Is a good solution? if i pass some object to the thread, this thread, this thread has to delete memory that it doesnt alloc.

For example:

The users call method sends

send(message)

I have a intern queue and if is full throws a callback

if (queueIsFull()){
callbackQueueIsFull(message not sent)
}


if the user implements his method

void Interface::onQueueFull(message not sent){

while true{
trying to send message
}


While the user is trying to resend my library is going to be stopped, and perhaps i'll have more messages to send or more to receive.

Which is the best way to do this? events?

thanks!

There is no programmable fix for stupid. The programmer of the application which uses your library will probably find it out if he attempts to do things as dumb as you described. Good function documentation should be all that you need to do. See Narue's avatar :)

Yes, while true was only an example. But, if the user takes 10 ms to execute the implementation of his method, my library will be stopped this 10 ms, and in this 10 ms I could have process more messages (send/receive), any suggestion?

The only suggestion would be to processess the messages in another thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.