Click here to Skip to main content
15,886,689 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: need help Pin
ahmedbhatti8-May-09 13:43
ahmedbhatti8-May-09 13:43 
AnswerRe: need help Pin
Hamid_RT8-May-09 0:44
Hamid_RT8-May-09 0:44 
GeneralRe: need help Pin
ahmedbhatti8-May-09 9:28
ahmedbhatti8-May-09 9:28 
Questionstream Pin
viliam7-May-09 9:59
viliam7-May-09 9:59 
QuestionHow to run an exe in admin mode? Pin
birajendu7-May-09 9:38
birajendu7-May-09 9:38 
AnswerRe: How to run an exe in admin mode? Pin
Madhu Nair7-May-09 9:53
Madhu Nair7-May-09 9:53 
AnswerRe: How to run an exe in admin mode? Pin
David Crow7-May-09 10:00
David Crow7-May-09 10:00 
QuestionMultithreaded code and the volatile qualifier Pin
Skippums7-May-09 9:17
Skippums7-May-09 9:17 
I have written a multithreaded application, and am wondering if I need to declare any of the shared variables as volatile. I have searched online for a while, but have thus far found no definitive source on when you need to use this qualifier. My code operates similarly to the following:

#include <windows.h>
#define ITER_COUNT 300
#define BUFF_SIZE  10

class Buffer {
public:
    const size_t length;
    const HANDLE canWrite;
    const HANDLE canRead ;
    double* data    ;
    size_t  writeIdx;
    size_t  readIdx ;

    Buffer(size_t maxSize) :
        data(new double[maxSize]()),
        writeIdx(0), readIdx(0), length(maxSize),
        canWrite(CreateSemaphore(NULL, static_cast<LONG>(maxSize),
                 static_cast<LONG>(maxSize), NULL)),
        canRead (CreateSemaphore(NULL,             0             ,
                 static_cast<LONG>(maxSize), NULL)) { }

    virtual ~Buffer() {
        delete[] data;
        CloseHandle(canWrite);
        CloseHandle(canRead );
    }
};

void ThreadFunc(void* var) {
    Buffer& buffer(*static_cast<Buffer*>(var));
    for (size_t idx = 0; idx < ITER_COUNT; ++idx) {
        WaitForSingleObject(buffer.canWrite, INFINITE);
        for (size_t j = 0; j < 5; ++j) {
            buffer.data[buffer.writeIdx] += idx + j;
        }
        ReleaseSemaphore   (buffer.canRead, 1, NULL);
        buffer.writeIdx  = (buffer.writeIdx + 1) % buffer.length;
    }
}

void main() {
    double result = 0;
    Buffer buffer(BUFF_SIZE);
    HANDLE threadHandle = (HANDLE)_beginthread(&ThreadFunc, 0, buffer);
    for (size_t idx = 0; idx < ITER_COUNT; ++idx) {
        WaitForSingleObject(buffer.canRead, INFINITE);
        result += buffer.data[buffer.readIdx];
        buffer.data[buffer.readIdx] = 0;
        ReleaseSemaphore   (buffer.canWrite, 1, NULL);
        buffer.readIdx   = (buffer.readIdx + 1) % buffer.length;
    }
}
Clearly the const members need not be volatile, and the read/write indices are only read or modified in a single thread, so those are ok. However, do I need to declare the Buffer::data pointer as volatile or not?

I have another multithreaded application that has n > 1 threads, such that each thread modifies some data prior to exiting, but does so within a mutex-protected bit of code. Should this variable be declared as volatile as well? Thank you in advance for any assistance.

Sounds like somebody's got a case of the Mondays

-Jeff

modified on Thursday, May 7, 2009 3:41 PM

AnswerRe: Multithreaded code and the volatile qualifier Pin
Stuart Dootson7-May-09 10:48
professionalStuart Dootson7-May-09 10:48 
AnswerRe: Multithreaded code and the volatile qualifier Pin
cmk7-May-09 17:09
cmk7-May-09 17:09 
Questionany problems with this code Pin
kaku_lala7-May-09 7:51
kaku_lala7-May-09 7:51 
AnswerRe: any problems with this code Pin
David Crow7-May-09 8:56
David Crow7-May-09 8:56 
QuestionHELP [modified] Pin
yunpil7-May-09 7:40
yunpil7-May-09 7:40 
AnswerRe: HELP Pin
Cedric Moonen7-May-09 7:53
Cedric Moonen7-May-09 7:53 
JokeRe: HELP Pin
sashoalm7-May-09 22:39
sashoalm7-May-09 22:39 
QuestionRe: HELP Pin
CPallini7-May-09 9:31
mveCPallini7-May-09 9:31 
AnswerRe: HELP Pin
Wes Aday7-May-09 10:15
professionalWes Aday7-May-09 10:15 
AnswerRe: HELP Pin
Hamid_RT8-May-09 0:46
Hamid_RT8-May-09 0:46 
QuestionRasGetCustomAuthData fuction not working in non admin user. Pin
birajendu7-May-09 6:41
birajendu7-May-09 6:41 
QuestionCInternetSession with Authentification Proxy Pin
sashoalm7-May-09 6:06
sashoalm7-May-09 6:06 
AnswerRe: CInternetSession with Authentification Proxy Pin
Stuart Dootson7-May-09 7:32
professionalStuart Dootson7-May-09 7:32 
GeneralRe: CInternetSession with Authentification Proxy Pin
sashoalm7-May-09 22:37
sashoalm7-May-09 22:37 
GeneralRe: CInternetSession with Authentification Proxy Pin
Stuart Dootson7-May-09 22:42
professionalStuart Dootson7-May-09 22:42 
GeneralRe: CInternetSession with Authentification Proxy Pin
sashoalm8-May-09 0:34
sashoalm8-May-09 0:34 
GeneralRe: CInternetSession with Authentification Proxy Pin
Stuart Dootson8-May-09 2:04
professionalStuart Dootson8-May-09 2:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.