C++ oddness and I need some insight/help. Topic is solved

This forum is reserved for everything you want to talk about. It could be about programming, opinions, open source programs, development in general, or just cool stuff to share!
leiradella
I live to help wx-kind
I live to help wx-kind
Posts: 172
Joined: Sun Sep 07, 2008 9:49 pm
Location: Rio de Janeiro, Brazil

Post by leiradella » Fri Jul 24, 2009 3:22 pm

Dark Alchemist wrote:I still would like to be enlightened if you know how I could do multi-threads without it being in a global space on Windows.
It really depends on what you're trying to do. Threads usually work with their own local variables allocated in their own stack or dynamic allocated space from the heap. Assuming you're using CreateThread to, well, create new threads, you can also pass a pointer to whatever you want via the lpParameter parameter of the function (see CreateThread at MSDN.)

If you need global variables to change information between threads, you better use mutexes and/or other synchronization mechanisms specifically designed for this because it's not thread-safe to read from/write to global variables from different threads without a lock to ensure atomic reads and writes.

Cheers,

Andre

phlox81
wxWorld Domination!
wxWorld Domination!
Posts: 1387
Joined: Thu Aug 18, 2005 7:49 pm
Location: Germany
Contact:

Post by phlox81 » Fri Jul 24, 2009 7:54 pm

Dark Alchemist wrote:
phlox81 wrote:Don't use global variables.
Also static functions are only few times helpful.
Static must be used in windows for doing threads or at least that is everything I have read says.
Sorry but thats just bullshit.
I've done severalt threading applications on Windows, and wxThread does not require anything static.
Nor Boost::thread, nor QThread.

phlox

leiradella
I live to help wx-kind
I live to help wx-kind
Posts: 172
Joined: Sun Sep 07, 2008 9:49 pm
Location: Rio de Janeiro, Brazil

Post by leiradella » Fri Jul 24, 2009 11:55 pm

phlox81 wrote:
Dark Alchemist wrote:
phlox81 wrote:Don't use global variables.
Also static functions are only few times helpful.
Static must be used in windows for doing threads or at least that is everything I have read says.
Sorry but thats just bullshit.
I've done severalt threading applications on Windows, and wxThread does not require anything static.
Nor Boost::thread, nor QThread.

phlox
Yup, it is bull shit. the static keyword changes the scope of the declared function to be local to the object file it's compiled into, i.e. it won't be visible to the linker. Everyone uses it a lot to avoid bloating the global scope with names not meant to be globally visible.

Unless he's talking about static class methods, that for sure need to be static because the hidden this pointer won't be automatically sent to the method when the thread starts and you get a nice crash.

Cheers,

Andre

protocol
Moderator
Moderator
Posts: 680
Joined: Wed Jan 18, 2006 6:13 pm
Location: Dallas, TX
Contact:

Post by protocol » Sat Jul 25, 2009 4:19 am

It seems like there is a party in this thread. So I will add my two cents.

Have you tried using events to provide data to the threads?
/* UIKit && wxWidgets 2.8 && Cocoa && .Net */
QuRegExmm
wxPCRE & ObjPCRE - Regex It!

Dark Alchemist
Super wx Problem Solver
Super wx Problem Solver
Posts: 347
Joined: Wed Nov 02, 2005 10:33 am

Post by Dark Alchemist » Sun Jul 26, 2009 8:07 am

phlox81 wrote:
Dark Alchemist wrote:
phlox81 wrote:Don't use global variables.
Also static functions are only few times helpful.
Static must be used in windows for doing threads or at least that is everything I have read says.
Sorry but thats just bullshit.
I've done severalt threading applications on Windows, and wxThread does not require anything static.
Nor Boost::thread, nor QThread.

phlox
Since we have cursing then how in the EFF do you do windows threading, no not wxthreads, but windows threading otherwise?

Code: Select all

DWORD WINAPI Thread(LPVOID lpParam)
That is what Windows wants for CreateRemoteThread and CreateThread. I have tried shooting it XXX::Thread before but it either never worked or crashed since it doesn't like thread created in the namespace way. http://msdn.microsoft.com/en-us/library ... 85%29.aspx

Dark Alchemist
Super wx Problem Solver
Super wx Problem Solver
Posts: 347
Joined: Wed Nov 02, 2005 10:33 am

Post by Dark Alchemist » Sun Jul 26, 2009 8:10 am

protocol wrote:It seems like there is a party in this thread. So I will add my two cents.

Have you tried using events to provide data to the threads?
No, I haven't but I never got around my include issue so in this project I am stuck with just including the .cpp and leaving it out of the project window. It works and for now that is all I care about (time issues). Why it wants it like that I don't know why but I got tired of fighting it for 3 or 4 days.

Post Reply