|
thanks. A friend of mine had a little .exe that he got off of planetsourcecode.com that allows you to do what I was talking about. It seems to work fine. Since I have one, there's no need to write one now!!;P
[insert witty comment here]
bdiamond
|
|
|
|
|
I've got a VC++ program in which I need to determine the dotted-numeric IP address of the PC its running on. What's the best/simplest way to do this?
Thanks for any ideas,
Craig
|
|
|
|
|
Call gethostname() followed by gethostbyname() .
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
|
|
|
|
|
Ok...can anyone help me with this problem. I"m havinng troulbe to figure out how to use the data members and fill them both completely with zeros while initiallize them in the default constructor.
Here is what I have so for the my header (.h) file...
class ConnectFour
{
private:
//date members
int board[12][13];
int numChips[7];
public:
ConnectFour();
//default constructor
};
And here is what I have in my .cpp file....
ConnectFour::ConnectFour()
{
board[12][13] = 0;
numChips[7] = 0;
for(int i=0; i < 12; i++)
{
for(int j=0; j<13; j++)
{
board[i][j] = 0;
}
}
for(int i=0; i<7; i++)
{
numChips[i]= 0;
}
}
IS THIS RIGHT?....BELOW IS HOW IT IS SUPPOSED TO BE DONE ON THE DIRECTIONS FOR THIS PROGRAM!
THANKS GUYS ..YOU GUYS SAVE MY LIFE
Data Members:
1.)a 2-dimensional array to hold the board – To make it easier to check for wins you chould use a board that has 12 rows and 13 columns. Only rows 3 – 8 and columns 3 – 9 will actually hold the board (which has 6 rows and 7 columns). The remaining cells will always contain zeros.
2.)a single-dimensional array of 7 ints to hold the number of chips dropped into each column of the board.
Methods
1.)a default constructor to initialize both arrays – filling them both completely with zeros
|
|
|
|
|
(duh to myself )
board[12][13] = 0;<br />
numChips[7] = 0;
Think harder, how are arrays indexed in C++ ( or C ).
you're getting there ... hang tough ...
Maximilien Lincourt
Your Head A Splode - Strong Bad
|
|
|
|
|
Maximilien wrote:
(duh to myself )
It's been a hard day ??
|
|
|
|
|
ISUstudent wrote:
board[12][13] = 0;
numChips[7] = 0;
Remove this two lines: you are writing a zero outside the arrays (numChips[6] is the last cell of your array because it is zero indexed).
Otherwise it is correct.
|
|
|
|
|
And, does it work? Looks good so far. You may also want to use memset.
memset( board, 0, sizeof( board ) );
Hardy.
|
|
|
|
|
Instead of the for loops, you could simply call memset() . Other than that, what you have so far is okay.
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
|
|
|
|
|
sweet. i just didn't know if i could have for loops and such in my defalut constructor
|
|
|
|
|
I have a Linked list with the following functions:
Add, Get, Delete.
For thread safe implementation I use a CRITICAL_SECTION handle in each function and everything work well.
The question is:
How I can avoid use of CRITICAL_SECTION on Get function?
Because Get function is called on many threads the performance is low, each thread wait the previous thread to finish Get.
Add and Delete are called rarely.
Thanks!
|
|
|
|
|
yes you can
I've already thought about this problem, too, and I found a very fast solution.
Lock the list by using interlocked functions, which use a LONG member variable of the class (I assume you use a list class) to synchronize access.
Don't try it, just do it!
|
|
|
|
|
I have used 2 interlocked vars and a semaphore. Below is a schematic of the code.
In this case the Get Thread has a very high access on the resource at 2 - 3 mil. of gets/sec
towards 20 - 50 of add and del /sec
If you have a better idea please tell me.
Thank you.
Get()
{
InterlockedIncrement(&_read);
while(_write > 0) Sleep(0);
CList::Get();
InterlockedDecrement(&_read);
}
Add()
{
while(_read > 0) Sleep(0);
InterlockedIncrement(&_write);
WaitForSingleObject(_sem,INFINITE);
CList::Add();
ReleaseSemaphore(_sem,1,NULL);
InterlockedDecrement(&_write);
}
Delete()
{
while(_read > 0) Sleep(0);
InterlockedIncrement(&_write);
WaitForSingleObject(_sem,INFINITE);
CList::Add();
ReleaseSemaphore(_sem,1,NULL);
InterlockedDecrement(&_write);
}
|
|
|
|
|
You can safely eliminate the CRITICAL_SECTION in your Get function if you make sure that your program is playing by the rules: as many threads can have read-only access to the list as long as none of them does an insertion or deletion. This usually involves some other synchronization mechanisms, although at a higher level than on the linked list itself, which presumably will yield better performance.
If you cannot guarantee this behavior in your program, another possibility is to protect access by means of a read/write lock. Such a lock allows multiple threads to enter for read access, except if some thread has reclaimed write access. A web search gives the following article[^] by Ivan Krivyakov on implementing read/write locks in Windows.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Hi.
A fast way to ensure synchronised access and update, is to use a simple 'mutex' that all three access/update methods conform to, ( as long as it is not a multi processor platform ).
If you do not care about the specific order in which access and/or update is performed, ie) its a free for all, then a simple WaitForSingleObject() on the mutex should provide a simple and fast mechanism.
James.
|
|
|
|
|
I'm afraid I'm not getting your point. If you protect every function of the list (Get , Add and Delete ), with a mutex, you'll get esentially what the original poster has already done with a CRITICAL_SECTION , with the added penalty that mutexes are heavier. My hunch is that the original poster asks for a way to allow many concurrent threads to be granted read access at a time, and not in a strictly one-by-one basis. Do you think this can be done with mutexes?
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Hi.
By using a simple mutex, access and update can be synchonised very easily.
When the list needs to be updated, the mutex is 'set', the update is performed, this excludes all access's, the update is complete and the mutex is 'released'.
Since the majority of the operations, as per the 'poster' are accesses, waiting for the mutex is infrequent.
Since for the majority of the time, the mutex is 'released' the wait for reads' is satisfied 'immediately'
James.
|
|
|
|
|
I'm still not getting you. AFAIK the only way to set a Win32 mutex is by waiting on it; releasing is done via ReleaseMutex . So your proposal is that the update function properly waits for the mutex and releases it after updating is completed, right? So, what should the access (i.e. read-only) function do?- if the access function also waits for the object, then there cannot be more than one thread either updating or reading at a time, exactly the same way as it happens with using a
CRITICAL_SECTION . - if the access function does not wait for the mutex, the list is not thread safe, as when the update method enters there could be other threads performing read-only acess at that time, and corruption of the data structure can hence ensue.
Maybe I am not getting your proposal right
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Hi.
Sorry, I assumed that there were multiple threads waiting on 'access and/or update', otherwise the question is void.
There can be as many threads waiting on the mutex as there is.
The idea is that only an update 'locks' the mutex, 'reads', 'fall through'. Otherwise we might be talking about a DOS program.
James.
|
|
|
|
|
Given:
Write the following code and discuss what exactly results.
1. enum logical { no, maybe, yes };
2. logical operator ! (logical x);
-------
So this gives me a set called logical. An I can define a variable like
logical x(no);
Item 2 is a template that takes x and returns NOT X. It is my opinion the template is meaningless. Not FALSE is TRUE, but NOT MAYBE is NO and NOT YES is also NO. Not NO is undefined.
Anyway, how can I use this template?
x = !(x);
Will just yeild a linker error? How do I define a usage of item #2?
|
|
|
|
|
Have you implemented the operator?
logical operator ! (logical x)
{
...
return ... ;
}
If not, you will have of course a linker error!
Hardy
|
|
|
|
|
First: an enum autoconverts in int , !1 is 0 and !0 is 1, hence - to avoid confusion, it is better use
{ no, yes, maybe } (maybe = 2)
now, if x is yes or no, already works.
The problem is !maybe = no (by definitionm in C++).
If this is unacceptable,
do an operator like
logical operator!(logical a)
{
return (a==maybe)? maybe: (logical)!(int)a;
}
please note the casts! (or you go in an infinite recursion !)
2 bugs found.
> recompile ...
65534 bugs found.
|
|
|
|
|
Thank you both. I was not casting properly.
My work product follows for anyone concerned.
//In C or C++ the following is true
//1. !0 is true and 0 is false
//2. !1 is false and 1 is true
//3. !Any number other than 0 is false and !0 is always true
enum logical { no, maybe, yes };
// no = 0 and !no is true
// maybe = 1 and !maybe is false
// yes = 2 and !yes is the same as !maybe
logical operator ! (logical x)
{
x = (logical)!(int)x;
return x;
}
logical operator &&(logical a, logical b)
{
logical x = no;
if(a &&(int) b)
{
x = yes;
}
return x;
}
void CTestinistuffDlg::OnCancel()
{
logical x(no);
logical a(maybe), b(yes);
//the Not operator
x = operator!(no); //returns maybe
x = operator!(maybe); //returns no
x = operator!(yes); //returns no
x = operator&&(no,no); //returns no
x = operator&&(no,maybe); //returns no
x = operator&&(no,yes); //returns no
x = operator&&(maybe,maybe); //returns yes
x = operator&&(maybe,no); //returns no
x = operator&&(maybe,yes); //returns yes
x = operator&&(yes,yes); //returns yes
x = operator&&(yes,no); //returns no
x = operator&&(yes,maybe); //returns yes
}
|
|
|
|
|
Hi,
I'm having some troubles with my MFC multi document application. When I create a new document, I would like it to fill the hole frame area. Is that possible, and how do I do it ?
With regards
jens Christiansen
|
|
|
|
|
Hi all,
I've created an ActiveX control using Visual C++ .NET.
When I add this control to a container created with Visual C++ .NET (or a Visual C++ 6.0) everything works and there is no apparently problem. But now, I'm trying to test this ActiveX with an application created with Visual Basic 6.0. The problem is that the ActiveX control does not appear in the list of registered components from the Visual Basic 6.0 (Menu -> Project -> Components, or Ctrl+T). So, I can't add my ActiveX to the Visual Basic 6.0 container.
When I try to make the same operation with a Visual Basic .NET container, I find the same situation, this is, the ActiveX control is not in the registered components list. But the ActiveX control is already registered.
I use the OleView application in order to ensure that the control is registered.
Due to my ActiveX control perfectly works with a Visual C++ .NET (and a Visual C++ 6.0) container, I suppose there is any bad chosen compiler option (by the time, everything is by default). Does anybody experimented the same situation testing his/her ActiveX in a Visual Basic container?
Thanks in advance.
|
|
|
|