C++ pthread concepts for solid applications?

Red Squirrel

No Lifer
May 24, 2003
71,304
14,081
126
www.anyf.ca
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)

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?
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Look at any variables that a thread is using/requesting that are not self contained.

Everyone is a potential hazard unless properly protected
 
Sep 29, 2004
18,656
68
91
Your example looks fine. If possible, use a locking method that has a time parameter incase another thread borked. After 10 seconds (for example), you might be able to assume the other thread is dead.

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?
Yes you can do this, but you will need more than one mutex. Unless there is a reason to do this such as performance there is no reason to.


My misc thoughts:
Put anything that is "shared memory" into it's own class. Very easy to do with Model/View/Controller where model is where all the shared memory goes. Have each getter/setter lock/unlock the mutex as needed.

Regarding you server question, that can cause some problems, sure. Encapsulate everything server related into it's own class.

It takes time but you will see the elegance when it is done. You might even smile noticing how well meshed everything is ;-)

Of course, this a simple solution. Depending on your needs, you might have to do something more complex.

Sorry if broken thought ... just got interupted and posting anyway.
 

Red Squirrel

No Lifer
May 24, 2003
71,304
14,081
126
www.anyf.ca
Looks like I'm more or less on the right path then. Good idea with putting shared resources in a single class, I may try that, will make it easier to control and track.

Another thing I was thinking, is there any base C/C++ functions that may depend on some internal global/static variables that cannot be used in a thread even on two separate objects?

For example, let's take string. Is there any operations I should not do to a string within a thread, because even though I'm dealing with separate objects, said operation may be using a static somewhere internally?

Just been going through my code over and over and there is no place where my own variables should be touched by another thread, but the crashes keep happening with internal C/c++ stuff, like right down to the very core libraries and such. Starting to wonder if some of my calls are failing due to an internal function not being thread safe or something.
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
Generally stdlib calls that manipulate memory you hold directly won't be a problem. Using things like iostream will generally be OK for reading, but not for writing. That's also going to be platform specific so I would just search around if you have any specific issues.

Also, you might find it easier to use the boost threading API's instead of pthread's directly. I find the syntax makes the code much easier to read and understand.