What are some must know concepts when writing multithreaded C++ apps?
I am working on something and it's crashing completely randomly, and I suspect it is something I'm doing wrong. I could post my code, but this is an instance of "teach a man to fish" vs "give a man a fish". I want to learn the concepts I need to know so I can try to fix it myself.
I understand that one crucial thing to keep in mind with multithreaded application is control access to shared resources. So say I have an array, or a single variable, that could be assed by more than one thread, then I need to use a mutex.
So I lock the mutex before accessing this resource, and unlock it after. So say I have 10 threads who try to access it, the one that gets to the lock first will succeed and get access, the others will "block" at the lock part, as it cannot lock the mutex as it's already locked. Then the thread that "won" will unlock the mutex, and the cycle continues. This should ensure that this resource is never accessed by more than one thread.
Usually this is what I do: (pseudo code)
Obviously that's not valid function/type names etc... but you get the idea. Is this the right way of handling a mutex?
Now that I got that part covered, is there anything else I need to keep in mind when writing multithreaded applications? Like, can I have a class that has a bunch of functions, and have one function be accessed by a thread while the rest of the class is also accessed by another thread? These functions arn't sharing any resources, but just the fact that I'm accessing the same class, can that cause issues?
What about networking and other stuff like that? If two threads try to connect to a server at the same time, can that somehow cause issues? Is there certain resources at the hardware level that I maybe need to use a mutex for or something?
I am working on something and it's crashing completely randomly, and I suspect it is something I'm doing wrong. I could post my code, but this is an instance of "teach a man to fish" vs "give a man a fish". I want to learn the concepts I need to know so I can try to fix it myself.
I understand that one crucial thing to keep in mind with multithreaded application is control access to shared resources. So say I have an array, or a single variable, that could be assed by more than one thread, then I need to use a mutex.
So I lock the mutex before accessing this resource, and unlock it after. So say I have 10 threads who try to access it, the one that gets to the lock first will succeed and get access, the others will "block" at the lock part, as it cannot lock the mutex as it's already locked. Then the thread that "won" will unlock the mutex, and the cycle continues. This should ensure that this resource is never accessed by more than one thread.
Usually this is what I do: (pseudo code)
Code:
//globals:
Mutex mutex;
Array array;
//Threaded function:
ThreadFunction()
{
mutex.Lock(); //if it's already locked, it will block here and wait
array.Add(item);
mutex.Unlock(); //release mutex
}
Obviously that's not valid function/type names etc... but you get the idea. Is this the right way of handling a mutex?
Now that I got that part covered, is there anything else I need to keep in mind when writing multithreaded applications? Like, can I have a class that has a bunch of functions, and have one function be accessed by a thread while the rest of the class is also accessed by another thread? These functions arn't sharing any resources, but just the fact that I'm accessing the same class, can that cause issues?
What about networking and other stuff like that? If two threads try to connect to a server at the same time, can that somehow cause issues? Is there certain resources at the hardware level that I maybe need to use a mutex for or something?
