3

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... ?

Matt Bryant
  • 4,841
  • 4
  • 31
  • 46
Rajan Chennai
  • 145
  • 1
  • 7

2 Answers2

9

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.

TypeIA
  • 16,916
  • 1
  • 38
  • 52
  • That's really nice. But consider a case where THREAD 1 writes value to the pointer and THREAD 2 only reads it. In that case, will my suggestion work ? – Rajan Chennai Dec 23 '13 at 17:00
  • 2
    There's a question in that case of *what* the value is, if it's a pointer to a complex type, you may find a partial write state (is your write *atomic*) It's safer simply to use a mutex to synchronize access to the resource. Typically if you have a complex type you can add the mutex *to* your type, another member of the struct, and operate on that, without having to pass a 2nd pointer around. – Lee Hambley Dec 23 '13 at 17:02
  • 1
    @RajanChennai: Only concurrent read-operations are safe without synchronisation! – alk Dec 23 '13 at 17:25
3

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.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547