1

I have yet to experience deadlock.
Apologies for the confusion. Ed wrote "deadlock" while thinking "race condition". Too much coffee, or not enough? ;)

Why is it more exception safe to read and pop separately? I can't think what the exceptions would be?
Here is readRemove() with the next logical step of generalizing the contained type.
[code]
template <typename T>
T multiQ<T>::readRemove()
{
boost::unique_lock<boost::mutex> lock(m_mutex);

if (!m_queue.size())
    m_cond.wait(lock);

// 1) T's copy constructor might throw
T returnVal = m_queue.front();

// 2) Never throws
m_queue.pop_front();

// 3) T's copy constructor might throw
return returnVal;

}
[/code]
The problem is the line marked 3. If the copy constructor throws an exception, the stack unwinds without passing returnVal back to the caller. But the queue's state has already changed, so the value is lost.

I would be interested to know where you would put try and catch blocks in this code (or some part of it).
There is no need for try/catch unless the function can safely recover. Pushing a value that was popped is not guaranteed to be a nothrow operation, so it is unsafe. The right way to go about fixing the problem is to avoid it in the first place instead of trying to clean up the mess after it happens.
[code]
void multiQ<T>::readRemove(T& returnVal)
{
boost::unique_lock<boost::mutex> lock(m_mutex);

if (!m_queue.size())
    m_cond.wait(lock);

// 1) T's copy constructor might throw
returnVal = m_queue.front();

// 2) Never throws
m_queue.pop_front();

}
[/code]
Everything after copying the value is a nothrow operation, ...

1

nbaztec, this is the C forum. C++ does not work here. ;)

I want a function which can return a string and also it should be able to take DEFAULT string arguments....
C does not support either default arguments or function overloading, so the simple answer is you cannot do it. The not so simple answer is you can probably do it with compiler extensions and/or tricks, but the end result is probably not worth the extra effort and complexity. You can read more on [URL="

2

I highly recommend you use read & write methods to store these data.
If any of the members are not POD, it is undefined behavior. If the file was written by a different program, padding between struct members might not be the same and memory corruption can happen. Lastly, the file cannot be a text file because the way you recommended is a straight byte copy directly into a punned chunk of memory.

The OP's examples suggest that the file is a text file and not binary. Also, std::string is not a POD type, so you cannot use daviddoria's struct with read() or write().

Edward does not recommend this practice. It is much better to use serialization techniques that are safe and established in C++. The simplest of these is a record class with overloaded << and >> operators.

3

I beg to differ going by OPs comments:
Then we will differ. Ed does not want to get into a pointless argument. The OP can pick whichever answer is a better fit, and ask for clarification as necessary. ;)

I assume your program prints it's own code using the FILE parameter which is the path to the source code
That is how it works, yes.

1

it's limited to specific programs where programmers intelligently type in the same code in strings
Not always, but that is the most common method in the absence of reflection. The simplest C++ quine that Ed has come up with does not use that method.
[code]

include <fstream>

include <iostream>

int main()
{
std::ifstream ifs(FILE);

if (ifs)
    std::cout << ifs.rdbuf();

}
[/code]

though the OP wanted something else
Edward respectfully disagrees. A quine is exactly what the OP asked for: a program that prints its own source code. A disassembler is not the same thing.

1

Have i missed/forgotton subtle points in my own program?
Actually, you nailed a subtle point by having readRemove() pop the item and return it. Good exception-safe design usually wants those two operations to be split, like std::queue<>'s front() and pop(), but in the presence of threads that can cause deadlock between the front() and pop() call. It is possible to avoid the risk of deadlock with that kind of one-two punch design, but just lumping it together like you did is simpler. Props. :)

1

What is the consequence?
Just lack of clarity. :) Edward needed a minute to verify that the condition was only used for a wait on empty, and when working with threaded code it is critically important to understand where locks and waits are because synchronization points are where problems pop up. It also couldn't hurt to minimize the number of notifications when they are not necessary. Less work and all that. ;)

2

Anyway the code is lengthy but I would appreciate comments from those more experienced in Multi-Threaded programming.
You should probably only call notify_one() if the previous state of the queue is empty. That's the point of using the condition variable, right? To avoid popping from an empty deque? ;)
[code]
void add(int i)
{
boost::mutex::scoped_lock lock(m_mutex);
bool first_item = m_deque.empty();

m_queue.push(data);

if (first_item)
    m_cond.notify_one();

}
[/code]
Edward would like to hear your reasons behind the design of readRemove(), because there's some subtle stuff going on that Ed usually doesn't see from people claiming to be inexperienced in multithreaded programming. ;)

I haven't tried it in C# yet although I think it may be easier as there is a sychronized queue already implemented?
The old containers library in C# has a Queue.Synchronized() method, but it's really no better than basic locks, which C# supports natively. Ed recommends keeping it simple with the lock keyword.

Votes + Comments
Knows stuff!
2

I'm not saying your style's wrong; but it's better to slim down on your lines of code.
The two styles do different things. The first style only makes the specified names available. The second style makes all names in the namespace available. It is easier to get name collisions with the second style. Ed recommends avoiding the second style unless you are careful not to use standard names or have a naming convention that guarantees no collisions with the std namespace.

Votes + Comments
Yep
good explanation
1

how i can develop my own programming language that will fullfills all the requirements of the present industry
Scala already exists, and it's a beast. :D It sounds like you want to create a real language as opposed to a toy for learning language design and implementation. Start by finding a need for it. Lots of people write their own pet language that never takes off because there's no reason to use it.

1

Edward would like to master the basics... Nothing is more marketable than a developer with super sturdy foundations.

So now I'm thinking "What now"?
Now you start writing software. There are system APIs and GUI APIs, and tools like Boost that will keep you busy learning for a long time. Try joining an open source project or getting an internship. The only way to really understand what you need to know is to hit roadblocks while coding that force you to learn new concepts, tools, and techniques. :)

1

It there any way to telll the compiler that from this to that point, everything I implement belongs to a certain class?
There is no way the compiler knows that you are implementing a member outside of a class definition unless the name is qualified. The only way to avoid qualifying definition names is to embed them in the class definition.
[code]
class Foo {
public:
// No qualification needed
Foo() { cout << "Foo()\n"; }

// Qualification will be needed
Foo(int);

};

// See? Qualification was needed
Foo::Foo(int) { cout << "Foo(int)\n"; }
[/code]
But beware embedding method definitions in the class. It makes them inline, and inline methods can be [URL="

1

getline()'s first parameter is an istream reference. It looks like you want an intermediate string stream.
[code]

include <fstream>

include <iostream>

include <string>

include <sstream>

include <cstdlib> // For exit()

int main()
{
std::ifstream arquivo("entrada.txt");

if (!arquivo)
{
    std::cout << "File not found\n\n";
    std::system("PAUSE");
    std::exit(EXIT_FAILURE);
}
else
{
    std::cout << "Loading file...Please wait...\n\n";
}

std::string arquivo_linha, arq_linha_part;

while (getline(arquivo, arquivo_linha))
{
    if (arquivo_linha[0] != '#')
    {
        std::istringstream token(arquivo_linha);

        while(std::getline(token, arq_linha_part, ':'))
        {
            std::cout << arq_linha_part << std::endl;
        }
    }
}

}
[/code]
Ed also cleaned up the code a bit. Apologies. :$

4

In anticipation of the thread being split, Ed has a comment.

I suggest using a bitwise AND (much quicker as it doesn't involve a division)
This is a common misconception. Every time Edward tries to confirm such claims, the result is that the two are competitive at all optimization levels. While bitwise AND is statistically faster when averaging large numbers of tests, the actual difference in each test is vanishingly small.

Ed's conclusion is that it doesn't matter and you should pick whichever option is clearer. :)

Votes + Comments
Good going Ed
good sumation
1

Template parameters are not polymorphic that way. One way to do what you want is to put identifying information in the base class and require that information in the container class.
[code]
class Base {
public:
enum { BASE_TYPE }; // Identifying information
virtual ~Base() {}
};

class Derived : public Base {
public:
virtual ~Derived() {}
};

template <typename T>
class Container {
enum { SafeType = T::BASE_TYPE }; // Require identifying info to exist
};

int main()
{
Container<Base> c1;
Container<Derived> c2;
Container<int> c3; // Error, yay!
}
[/code]

1

how can i avoid the 0 z in the value that is being displayed.
You cannot. This is a limitation of the MaskedTextBox. Edward usually uses a regular TextBox and manages the format manually. For example, just a straight validation is super easy.
[code]
private void textBox1_Validating(object sender, CancelEventArgs e) {
decimal value;

if (decimal.TryParse(textBox1.Text, NumberStyles.Currency, null, out value)) {
    // Do something with the value
} else {
    // Notify the user somehow
    e.Cancel = true;
}

}
[/code]
By handling text box events you can do some cool stuff. The MaskedTextBox is really just a convenience if you do not need absolute control and do not mind the formatting limitations.

Votes + Comments
This is the correct way.
1

@tennis

  1. an int and a class with only an int member, are they of the same size?
    Maybe. The size of a class will be the total bytes of the data members and any padding. It is the padding that gets you on this question.

  2. what is the size of a class containing 4 ints and a virtual function?
    The size of 4 ints, plus padding, plus whatever storage is needed for the virtual mechanics used by the compiler.

@venkat arun

2) I don't think functions occupy memory
Functions are not stored in each object, but virtual functions usually mean that each object gets a vtable, and that adds to the size of each object.

1

why they are using fp
Edward assumes you mean why is it [CODE]FILE
fp = fopen(...)[/CODE] instead of [CODE]FILE fp = fopen(...)[/CODE]
Under the hood, it looks kind of like the following.
[code]
struct FILE {
HANDLE h; / System file handle /
BUFFER b; / I/O buffer /
FLAG f; / State flags /
};

FILE _files[FOPEN_MAX];
int _curr_file = 0;

FILE fopen(...)
{
FILE
fp = NULL;

if (_curr_file < FOPEN_MAX) {
    fp = &_files[_curr_file++];
    fp.h = /* Get a system file handle */
    fp.b = GetIoBuffer();
    fp.f = IO_STATE_GOOD;
}

return fp;

}
[/code]
But why does fopen return a pointer? The biggest reason in Ed's opinion is that a FILE object's members need to be changed and persist across function calls. Building pass by reference directly into the type instead of leaving the onus on programmers to get it right makes everything easier. :)

1

There is probably a .NET method for this, but Edward's first thought was to do it manually with a dictionary.
[code]
public string MostFrequent(string[] arr)
{
var freq = new Dictionary<string, int>();

Array.ForEach(arr, s => freq[s] = freq.ContainsKey(s) ? freq[s] + 1 : 1);

return (from x in freq orderby x.Value ascending select x).Last().Key;

}
[/code]
There is probably a more efficient way, and probably a shorter way, but that is what Ed came up with on short notice. ;)