// JP opened flex table

Click to See Complete Forum and Search --> : Threads


NChan
November 9th, 2001, 08:48 AM
Hi!
I am new to threads and I have a couple of questions about basic things about threads. If you have something like this without any critical sections or any other kind of synchronous object around it, what will happen.


int x, y;
func(x, y);/* a function which takes these two variables and modifies them*/
cout<<x<<y;



Will there be any problems? Wouldn't each thread make a copy of x and y and use that? So, is a critical section really needed? If not, when should the critical sections be really needed?

Also, is there any good sites on the net with basic stuff about this?

Thanks a lot,


NC

Andreas Masur
November 9th, 2001, 09:32 AM
If the code you gave is the thread function, then there won't be a problem at all. But this is not exactly a situation for a multithreaded application.

Multithreaded applications usually deal with some memory they share amongst several threads. Think of an application where one thread updates periodically a variable while another thread is doing some calculation with exactly the same variable periodically. In this case you need to take care that not both thread will access this variable at the same time. In this case you would use a mutex semaphore to control the access.

Ciao, Andreas

"Software is like sex, it's better when it's free." - Linus Torvalds

//JP added flex table