Thread program

T

takeshi honda

I'm trying to create a thread program according to this article.
http://www.codeproject.com/Articles/21114/Creating-a-C-Thread-Class

The sample code is a case that each thread processes easy quick tasks.
In this case I have no problem.
But I want two threads to do time consuming process such as recvfrom().
In this case threading program doesn't work well.

while(1){
thread1.Event(); // this is waiting recvfrom() return.
thread2.Event(); // this doesn't start until above process finishes.
}

I also tried ThreadTypeIntervalDriven as follows.

thread1.SetThreadType(ThreadTypeIntervalDriven,10);
thread2.SetThreadType(ThreadTypeIntervalDriven,10);

For some reason, this causes a crash of the program.

How can I create program which has two threads using recvfrom()?
 
R

Robert Miles

I'm trying to create a thread program according to this article.

http://www.codeproject.com/Articles/21114/Creating-a-C-Thread-Class

The sample code is a case that each thread processes easy quick tasks.

In this case I have no problem.

But I want two threads to do time consuming process such as recvfrom().

In this case threading program doesn't work well.


while(1){
thread1.Event(); // this is waiting recvfrom() return.
thread2.Event(); // this doesn't start until above process finishes.
}


I also tried ThreadTypeIntervalDriven as follows.


thread1.SetThreadType(ThreadTypeIntervalDriven,10);
thread2.SetThreadType(ThreadTypeIntervalDriven,10);


For some reason, this causes a crash of the program.

How can I create program which has two threads using recvfrom()?

There's another newsgroup, comp.programming.threads, which may be appropriate
for this question.
 
J

Jorgen Grahn

Have you ever had a look at the C++11 standard for threads? That article
seems obsolete.

Not so much because it doesn't rely on the C++11 standard -- you can't
expect everyone to use it already. But the code seems archaic at a
glance (BOOL, CThread, no namespace ...) so I'm skeptic too.

Surely there are better pre-C++11 examples out there. Boost?

/Jorgen
 
Ö

Öö Tiib

I'm trying to create a thread program according to this article.
http://www.codeproject.com/Articles/21114/Creating-a-C-Thread-Class

I do not like the implementation at all. Reasons:
* No namespaces, all that crap goes to global namespace
* Naming conventions especially Hungarian crap like "m_dwThread" and even both prefix and verbose
telling what it is like "CMutexClass"
* No separate RAII Lock class instead there are separate Lock and Unlock methods of mutex, so no exception safety.
* etc.

You will learn bad habits from that code, delete it.
If your compiler has std::thread then use it.
If it does not have download boost and use boost::thread.
How can I create program which has two threads using recvfrom()?

What you seem to need for that is actually boost::asio ... learn to use it.
 
P

Paul N

I do not like the implementation at all. Reasons:
* No namespaces, all that crap goes to global namespace
* Naming conventions especially Hungarian crap like  "m_dwThread" and even both prefix and verbose
  telling what it is like  "CMutexClass"

Sorry if this is a naive question, but - I thought the point of
namespaces was to avoid name clashes, so why does it matter that the
names are in the global namespace if they're horrible ones which you
presumably aren't going to want to use yourself?
 
I

Ian Collins

Sorry if this is a naive question, but - I thought the point of
namespaces was to avoid name clashes, so why does it matter that the
names are in the global namespace if they're horrible ones which you
presumably aren't going to want to use yourself?

Some other library may be foolish enough to use them?

I agree the code looks like some awful 90s MFC mess.
 
J

Jorgen Grahn

[several other reasons snipped by PN]
Sorry if this is a naive question, but - I thought the point of
namespaces was to avoid name clashes, so why does it matter that the
names are in the global namespace if they're horrible ones which you
presumably aren't going to want to use yourself?

You mean it's ok to skip namespaces if you choose really awful names
for everything?

Apart from that: namespaces aren't just for avoiding name clashes.
They also /group/ related names.

/Jorgen
 
W

woodbrian77

I gather you are kind of flagellant person wanting to invent and use

horrible ugly names in your everyday work. Unfortunately, most people are

more comfortable and would like to use normal meaningful names instead.

For example, the above link contains code for a class named CTask, which

is not so horrible (sans the meaningless C prefix) and has a real danger

of colliding with a name in another library of a similar mindset.



Namespaces solve this dilemma quite neatly. You just put everything in a

namespace which is unique/ugly enough and use short meaningful names

inside the implementation in the namespace; in other code using this

library you can use short namespace aliases or using directives

(discouraged). E.g:


Is there a need for an on line registry of namespaces?



Brian
Ebenezer Enterprises -- making programming fun again.
http://webEbenezer.net
 
T

takeshi honda

Thank you everyone.

Unfortunately, I do program in severe environment where neither std::threadnor boost exists. I am not an admin of the server.

Do you have any idea for this solution?

2012年11月3日土曜日 22時51分25秒 UTC+9 takeshi honda:
 
W

woodbrian77

No, we do not need another ICANN, if that's what you mean. If at worst all

7G people suddenly started to code in C++, then we could just insert UUIDs

in all namespace names.

I don't like the idea of adding UUID to names.
Names should be meaningful.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top