Synchronization & the join() method

Need help with C, C++, perl, python, etc?

Synchronization & the join() method

Postby maths_lover » 2006-10-04 06:34

Hi everyone,

I know that because we tend to avoid corruption of shared data by multiple threads, we use synchronization and that the join() method of the Thread class, doesn't let another thread to interrupt the current thread and waits till this thread finishes his work and dies.

My questions is that "When do we need to use both synchronization and the join method in our program?" I mean when we use synchronization surely the information of each thread will remain safe; so do you think there would be any need to use the join method in any part of the program for any reason??

please put more light on this behavior & clarify.
I would appreciate any information regarding this matter...
Thanks in advance,
Maryam
maths_lover
 
Posts: 1
Joined: 2006-10-04 05:46
Location: Iran

Postby lacek » 2006-10-05 14:37

join() is not used to avoid data corruption -- that's what synchronization is for. If you join() a thread to another, it simply causes the current thread to suspend its' execution until the other thread doesn't finish. But that's for the current thread only. Synchronization, on the other hand, assures that a given block isn't executed in the same time by two threads.
You need to use join when you want to make sure that a given thread's execution is suspended until an other thread finishes.
lacek
Moderator Team Member
 
Posts: 769
Joined: 2004-03-11 18:49
Location: Budapest, Hungary

Postby Guest » 2006-11-16 15:39

Building off of what lacek said, let's take an example of a simple main() function that spawns a few threads. If you do not do a join(), then it is very possible that main will return before all the threads finish, causing the program to exit while the threads are still running. So, you must use some form of thread synchronization in order to make things behave when using threads. I recommend you pick up a good book on POSIX threads before you get started.

joe
Guest
 


Return to Programming

Who is online

Users browsing this forum: No registered users and 6 guests

fashionable