hello...
i'm just playign around with multithreading a little bit before i get started on my real work with it.. so the code i'm goign to post doesn't really do anything useful other than let me experiment a little!
i want to use events. so, i create an event in the main method. now, i want access to that event in my new thread(s). i think the way to go about doing this is by using the security attribute parameter in CreateEvent(...).
I have teh following questions. they refer to the code that follows.
Questions:
1. If i want access to an event in my thread created in main below, do i actually have to use security attributes? or is there a simpler way of going about it? i tried to access the event in the thread, but i get compiler errors.
2. Assuming i do need to set the security attributes to do what i just described, how do i go about setting "lpSecurityDescriptor" (secatt.lpSecurityDescriptor in the code below).
3. What is the point of functions like OpenEvent and OpenMutex? Do they need to be called to gain access to an event/mutex from a child thread? MSDN just said they are used to open a mutex or event or whatever:
.. i'm not quite sure what they mean by "open". That is all they say about what the function does!
4. Is there an FAQ i shoudl be looking at instead of bothering you guys? because i couldn't find one.. also msdn is a little bit vague about lpSecurityDescriptor.
Thanks!
i'm just playign around with multithreading a little bit before i get started on my real work with it.. so the code i'm goign to post doesn't really do anything useful other than let me experiment a little!
i want to use events. so, i create an event in the main method. now, i want access to that event in my new thread(s). i think the way to go about doing this is by using the security attribute parameter in CreateEvent(...).
I have teh following questions. they refer to the code that follows.
Questions:
1. If i want access to an event in my thread created in main below, do i actually have to use security attributes? or is there a simpler way of going about it? i tried to access the event in the thread, but i get compiler errors.
2. Assuming i do need to set the security attributes to do what i just described, how do i go about setting "lpSecurityDescriptor" (secatt.lpSecurityDescriptor in the code below).
3. What is the point of functions like OpenEvent and OpenMutex? Do they need to be called to gain access to an event/mutex from a child thread? MSDN just said they are used to open a mutex or event or whatever:
From MSDN
The OpenEvent function opens an existing named event object.
.. i'm not quite sure what they mean by "open". That is all they say about what the function does!
4. Is there an FAQ i shoudl be looking at instead of bothering you guys? because i couldn't find one.. also msdn is a little bit vague about lpSecurityDescriptor.
Code:
#include <iostream>
#include <process.h>
#include <conio.h>
#include <windows.h>
int glob=3;
unsigned int __stdcall test(void* param)
{
//OpenMutex(NULL,temp,"tttt");
//std::cout << "from within thread" << std::endl;
std::cout << "stint is: " << (static_cast<char*>(param)) << std::endl;
//WaitForSingleObject(g_event,INFINITE);
// HANDLE yy=OpenEvent(NULL,g_event,"testevent");
std::cout<<"glob from thread is: " <<glob<<std::endl;
glob++;
return 0;
}
int main()
{
SECURITY_ATTRIBUTES secatt;
secatt.nLength=sizeof(SECURITY_ATTRIBUTES);
secatt.bInheritHandle=FALSE;
HANDLE g_event;
g_event=CreateEvent(NULL,TRUE,TRUE,"testevent");
//ResetEvent(g_event);
//WaitForSingleObject(g_event,INFINITE);
SetEvent(g_event);
static char* stint="static char";
std::cout<<"glob from main is: " <<glob<<std::endl;
_beginthreadex(0, 0, test, stint, 0, 0);
_getch(); // Give the thread some time here...
std::cout<<"glob from main is: " <<glob<<std::endl;
return 0;
}
Thanks!