Skip to main content
gamedev.net
🔒 Locked

threads

Started by Farfadet 4 replies 1.2k views
Original Post
Farfadet
Farfadet
I'm starting to use threads in my program, so I discover the joys of synchronization and the like. However, I could'nt find an answer to a rather basic question : can you call a procedure that's not in a thread, from 2 simultaneously running threads, or do you have to duplicate the code to make sure that the 2 threads won't collide ? I assumed the second proposal was right, so I'm instantiating objects in each of my threads so that each calls its own methods, but I'm not sure this is not a waste of code.
arthur83
arthur83
Quote:
Original post by Farfadet
I'm starting to use threads in my program, so I discover the joys of synchronization and the like. However, I could'nt find an answer to a rather basic question : can you call a procedure that's not in a thread, from 2 simultaneously running threads, or do you have to duplicate the code to make sure that the 2 threads won't collide ? I assumed the second proposal was right, so I'm instantiating objects in each of my threads so that each calls its own methods, but I'm not sure this is not a waste of code.


I'm not totally experienced on the subject but as far as I know there shouldn't be a problem for two threads using the same function, they are just executing it in it's own thread. You have to be carefull if the function uses a shared resource (like a global variable) so that it is not altered during the execution which may lead to unexpected results during execution and ofcourse be aware of deadlocks. Using local variables in the function is fine as it's stored on the stack when the thread is switched.
Sneftel
Sneftel
Quote:
Original post by Farfadet
can you call a procedure that's not in a thread, from 2 simultaneously running threads

Yes. After all, code is read-only, and there's no problem with multiple threads simultaneously reading the same data.
Samurai Jack
Samurai Jack
You have to be careful, the most important thing is: the method shall not use public or static variables. If you do use them, you need semaphores or critical sections.

so:
int doSomething(int value){return value*2;}

this is ok. this is not ok:
int doSomething(int value){static int oldval=0;oldval = ok*2;return ok + 5;}

or this is also not ok:
int _GlobalVariable = 0;int doSomething(int value){  _GlobalValue += value;  return _GlobalValue*2;}
Antheus
Antheus
You can call it.

But in order to safely call it (get predictable results), the function needs to be reentrant.
http://en.wikipedia.org/wiki/Reentrant

Note that many libraries are not. Unless the function you're calling clearly states that it's thread-safe, you should never use as such.

There's several solutions to this. One is done through locking, where each time you access a resource that is not thread-safe you use your threading implementation's locks. The other is through TLS (thread local storage) which allows static and other shared data to be accessed as if it were declared locally.

In general, thread safety is a somewhat broader term that implies correct handling other issues as well. It's a broad scope, so unless you know exactly why and how you're calling something outside of a thread, it's better not to do it. It can cause some very subtle and hard to fix bugs, or even worse, random unexplained failures.

Instantitating objects within each thread is fine, as is calling their methods, as long as these objects don't share any data that exists outside of a thread. If it does, same caution as above applies.

Farfadet
Farfadet
Thanks, this was most helpfull. I understand the answer is not simply yes or no. In my case, the threads are used for rendering a scene with openGL. I want to call my own rendering functions from within the threads, and those functions call openGL functions that are thread-safe, so if I got it right, all I need to do is take care that my functions don't write to public/static variables. Putting those functions in a class and instantiating different objects for each thread won't help, it will only make code heavier and eat up more memory ; what I have to do is avoid writing to static/public variables inside my functions, or protect the code where they do.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.