C++ threading

I'm writing an API that uses the ASIO SDK. In the SDK is has some callback functions that need to be implemented. They recommended that a separate thread be used for the callback mechanism. I'm fairly inexperienced with threading (but I'm sure I could learn it). I want to know is their a benefit to what they are recommending. Also note my API is in a DLL. And if I should use a separate thread should it be in another DLL?
They recommended that a separate thread be used for the callback mechanism.


That doesn't make much sense... as running a separate thread would just about defeat the entire purpose of having a callback mechanism. Are you sure they're not saying that the callbacks are called from a separate thread?

Can you link to where you read this?

Also note my API is in a DLL. And if I should use a separate thread should it be in another DLL?


No. You can have any number of threads in a single DLL.
I can't post their code because of the license but In a sample application that came with the SDK it says

// the actual processing callback.
// Beware that this is normally in a seperate thread, hence be sure that you take care
// about thread synchronization. This is omitted here for simplicity.


It says that on 2 of the 4 callback functions. Could this mean that all the data processing is usually done in another thread? That would actually make sense to me.
Last edited on
Beware that this is normally in a seperate thread, hence be sure that you take care


Yes... see... it's not saying that you need a separate thread... it's saying that the functions are called from a separate thread.

Could this mean that all the data processing is usually done in another thread?


Yes that is exactly what it means.



The synchronization here that it mentions is the tricky part -- and is the easy part to get wrong in your program. Be sure that any data used both in your main thread and in the callbacks is guarded behind some kind of sync mechanism. Usually this means a mutex.
Okay so it doesn't sound to bad then. So all I need to do is make a thread for processing the data when the callback functions is called. Well not make a new thread, I'll probably keep the same thread running but still.
So all I need to do is make a thread for processing the data when the callback functions is called. Well not make a new thread, I'll probably keep the same thread running but still.


You do not need to create any threads. The library is creating them for you.

When they call your callback function -- the code in your function will be running in a separate thread from the rest of your program.


1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
    // your main program thread
    //   Running in Thread A
}


void yourCallback()
{
    // Called by the library
    //    running in Thread B
}


Any thread can call any function, and what thread you're in depends on what thread called the function.


All you need to do is synchronize the data that is shared between your callbacks and your main program.
Last edited on
Oh okay! So when they call that callback function, it is called from thread B and anything that callback function calls is still in thread B. Correct?
Yes, that is correct.
I'm still wondering why I need to synchronize things though. The 2 processing callbacks have nothing to do with anything else going on. They only access buffers created by the the driver that the SDK decided to load.

Edit: And also the SDK has NOTHING to do with threading. If it creates a thread, it has to be done with the ASIO driver on your machine.
Last edited on
Data synchronization avoids race conditions when 2 or more threads try to access data at the same time.

1
2
3
4
5
6
7
8
9
10
11
12
13
int foo = 0;


void ThreadA()
{
    foo = 6;
}

void ThreadB()
{
    foo = 5;
    cout << foo; // Might output 5.  Might output 6.  Might output garbage.
}


Any data that is accessed by multiple threads* needs to be guarded somehow to prevent this kind of thing from happening.


*as long as 1 or more threads is writing. If all threads are doing read-only access, then sync is not necessary.


That said... if there is data that Thread B is using that is only being used in thread B... then that data does not need to be guarded. Likewise, data that is used only in Thread A does not need to be guarded.

Only data that is shared between threads needs to be guarded.
Okay thanks.
Topic archived. No new replies allowed.