Reputation: 145
Consider that i have two threads : THREAD 1 and THREAD 2. I just wanted to communicate between them. THREAD 1 contains a value which is stored in pointer and THREAD 2 needs that pointer value to be accessed around that thread.
As per my understanding, I think we can directly dereference a pointer from one thread to another. But my friend's suggestion is that I only access the value through IPC mechanism, not my suggestion.
Please comment on this. What is the best way to do and why... ?
Upvotes: 3
Views: 274
Reputation: 1
Read a good posix threads tutorial. You'll understand then why it is so important in practice to use synchronization primitives (at least to get understandable behavior from your program).
Any data can be shared between threads, because they all share the same common address space. However, you really want to synchronize such shared access (because you cannot know, without explicit synchronization, when one thread sees the changes done by another thread; read about cache coherence). A common way is to use mutexes for that.
To explain a bit, declare a global mutex with your shared global data:
pthread_mutex_t glob_mtx = PTHREAD_MUTEX_INITIALIZER;
static struct globaldata_st glob_data;
Then, to access some of the data, do e.g.
int sharednumber;
pthread_mutex_lock(&glob_mtx);
sharednumber = glob_data.number;
pthread_mutex_unlock(&glob_mtx);
And to atomically update that data, by incrementing it:
int sharednumber;
pthread_mutex_lock(&glob_mtx);
sharednumber = glob_data.number++;
pthread_mutex_unlock(&glob_mtx);
you would similarly serialize the update or access to a shared linked list queue, etc....
Don't be shy on using mutexes, they are quite fast. Always pair a pthread_mutex_lock
with a pthread_mutex_unlock
....
Remember that synchronization bugs are very hard to hunt because they are not reproducible: they are heisenbugs.
With GCC 4.8 on Linux/x86-64 you could use the thread sanitizer with gcc -Wall -fsanitize=thread -g
to ease debugging.
Upvotes: 3
Reputation: 17248
Two threads can access the same variable, that's fine. Just beware of synchronization issues. If both threads write the value, there is the possibility of a race condition. If either thread writes values non-atomically (such that the value of the data isn't in a consistent state at all times) there's the possibility of other threads reading the value while it's in an invalid intermediate state. These situations need to be handled with synchronization primitives like mutexes, semaphores, etc.
Upvotes: 9