Advertisement

threads and variable access

Started by July 03, 2002 06:14 PM
4 comments, last by shurcool 22 years, 7 months ago
i''m new to threads, and i have one serious (but easy for you) question. lets say we have the main loop A and a thread B. could i do the following, or do i have to use a mutex or something to lock data access? main A: { run thread B; while (!bServerRunning) { // more stuff // ... if (whatever) bServerRunning = false; } } thread B: { while (!bServerRunning) { // more stuff // ... if (whatever) bServerRunning = false; } } or do i have to lock a mutex before i check what''s IN bServerRunning (get its value) and before i change that value? or is that not necessary for something that simple, and [mutexes are] required only for complicated loops that include manipulating data, which could interfere with the same data another thread (or main loop) might also be manipulating? if the code i posted above is bad (ie. a mutex is needed) then i hate threads and this whole thing blows. /me walks out the door. but i have a feeling that i don''t need a mutex for that, which will make me happy (happier). please don''t disappoint me! --- shurcool my project
1. This has nothing to do with networking. You should probably try the general forum.

2. At the very least you need to declare bServerRunning as volatile. Otherwise your compiler may optimize it into non-existance.

3. Atomic updates (i.e. simple reads and writes) to primitive datatypes should work fine without doing anything special most of the time.

4. To be completely safe you should use the InterlockedXXX functions and/or a mutex or critical section anyway. Not using them probably won''t bite you but if someday somebody tries to run your code on some exotic architecture you may have problems.

-Mike
-Mike
Advertisement
Any time you access data from seperate threads it is necesary to protect the data with a mutex. It''s a good habit to get into, even for simple applications. Since both threads are running, you can''t guarantee that only one of your threads is going to touch that value at a given moment.
guys, do you realise that you just said two completely different things?

quote:

3. Atomic updates (i.e. simple reads and writes) to primitive datatypes should work fine without doing anything special most of the time.



vs.

quote:

Any time you access data from seperate threads it is necesary to protect the data with a mutex.



so... for my purposes? on a standard windows or linux platform, on a msvc compiler, whould that be fine? :-/
btw, with a mutex, code would look something like this, right?

while (true){    MutexLock(oMutex);        if (!bServerRunning)            break;    MutexUnlock(oMutex);    // ...    MutexLock(oMutex);        if (whatever)            bServerRunning = false;    MutexUnlock(oMutex);} 
---
shurcool
my project

[edited by - shurcool on July 4, 2002 9:48:25 AM]
No, you don''t need a mutex for that. The worst that can happen is that it''d set bServerRunning to false twice, which is of course, isn''t a problem really.

Sycronisation is only needed in cases where you need to actually serialize access to a piece of memory. A lot of the time, that''s just not needed.


codeka.com - Just click it.
quote:
Original post by Dean Harding
No, you don''t need a mutex for that. The worst that can happen is that it''d set bServerRunning to false twice, which is of course, isn''t a problem really.

Sycronisation is only needed in cases where you need to actually serialize access to a piece of memory. A lot of the time, that''s just not needed.


codeka.com - Just click it.


thank you very much! :D

---
shurcool
my project

This topic is closed to new replies.

Advertisement