POSIX threads

Hello !

Let's supose I have a main function in C , and two POSIX threads. I give you an example down :


int main() {
int something;
char else[];
void *FirstThread();
void *SecondThread();
 ..
<start those two pthreads ..>
return 0;}
void *FirstThread() { ... }
void *SecondThread() { ... }

First and Second thread will use something and else , int and char . Do thow two pthreads share the same space of memory ? I mean , First thread will modify the same something ( place of memory ) like Second Thread ?

Are you asking if you create two threads, can they modify the same memory?
Yes.
You prevent that by:

  1. keeping almost all the variables they change declared local to the function. That way they cannot hurt anybody else's data.
  2. For shared data, set up a mutex, so that only one function is able to change shared data at one time.

In your case, if you pass a reference to int and char to both functions, then you have a problem.

Oh yes , I figure it out.

Thanks again !