Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

Synchronization & the join() method

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
maths_lover
Posts: 1
Joined: 2006-10-04 05:46
Location: Iran

Synchronization & the join() method

#1 Post by maths_lover »

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

lacek
Posts: 764
Joined: 2004-03-11 18:49
Location: Budapest, Hungary
Contact:

#2 Post by lacek »

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.

Guest

#3 Post by Guest »

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

Post Reply