PDA

View Full Version : Unix threading


svero
01-05-2005, 04:33 AM
I have some threads being spawned under Win32. I want the code to be cross platform. Maybe someone who's very familiar with unix could tell me what the equivalent of doing this would be...

In Win32...

_beginthread (ThreadProc, 0, (void*)pData);

where ThreadProc is defined as...

void ThreadProc(void* pvData)

And contains the code to run for the new thread. pData is some data passed into the new thread, and the 0 argument is for the stack size for the new thread. In win32 you can passin NULL for that argument.

I read some stuff online but there seems to be different ways to approach it under unix. I guess im looking for the most standard friendly portable easy to use approach.

princec
01-05-2005, 05:23 AM
Java ;)

Actually: you'll be wanting to look at fork() I think.

Cas :)

elias
01-05-2005, 05:33 AM
fork() will create another process, separated from the original. On linux, the pthreads library is the standard.

- elias

svero
01-05-2005, 05:49 AM
Well... has anyone got an actual link to the details of how to use the fork() call and do what I'm already doing with the data passed in etc... I need more detail or some reference I can look at online.

Rod Hyde
01-05-2005, 07:14 AM
I read some stuff online but there seems to be different ways to approach it under unix. I guess im looking for the most standard friendly portable easy to use approach.
POSIX threads (pthreads) are fairly portable and straightforward to use. There is at least one implementation available for Windows, including one from RedHat.

The RedHat implementation of pthreads for Windows is here. It appears to be actively maintained as the latest snapshot was dated 2 days ago.
http://sources.redhat.com/pthreads-win32/

And there's an article on implementing thread pools using pthreads here:
http://www.codeproject.com/threads/threads.asp

--- Rod

GBGames
01-05-2005, 07:23 AM
I believe SDL has a portable threading subsystem, but I don't have experience with it. I believe I read that it is hard to do portable threading so the SDL threads are fairly basic.

svero
01-05-2005, 07:44 AM
Well I was hoping to just replace the _beginthread call with a unix call, but there seems to be no equivalent. Of course it's another complete nightmare. I dont quite get the fork call, or how one would pass data with it etc... I can think of a number of nasty swear words Id like to type regarding *nix thread support. What a mess.

The redhat thing seems ok, but i have to link in a 3rd party dll etc... and I really really really really don't want to get into that. Sigh...

ggambett
01-05-2005, 08:08 AM
Fork works more or less as follows :

if (fork() == 0)
{
// The spawned process continues running here
}
else
{
// The original process continues running here
}

The idea is that the fork() call creates a copy of the process, returns 0 to the recently spawned process, and returns the PID of the new child to the original process. For more info, see http://www.rt.com/man/fork.2.html

GBGames
01-05-2005, 08:37 AM
Well I was hoping to just replace the _beginthread call with a unix call, but there seems to be no equivalent. Of course it's another complete nightmare. I dont quite get the fork call, or how one would pass data with it etc... I can think of a number of nasty swear words Id like to type regarding *nix thread support. What a mess.


EDIT: I'm not familiar with _beginthread, but all my googling has turned up nothing but Windows-specific articles and posts. This leads me to believe that it is a Windows specific function. The following is my comment on that. If it turns out that _beginthread isn't Win32 specific, please let me know. /EDIT

So because Unix doesn't have something to drop in as a replacement for a function call on a system like Windows, which has APIs that generally aren't meant to be cross-platform, it's Unix's fault? Unix was originally designed to be multithreaded and multiuser. Windows was not.

I'd check out this book from a library:
http://search.barnesandnoble.com/booksearch/isbnInquiry.asp?userid=Xz6bOxDAP8&isbn=0201877589&TXT=Y&itm=4

It discusses fork and also threading on a POSIX compliant system (aka most Unix varients).

svero
01-05-2005, 08:41 AM
So because Unix doesn't have something to drop in as a replacement for a function call on a system like Windows, which has APIs that generally aren't meant to be cross-platform, it's Unix's fault?

I didnt mean to imply that. The frustration has to do with the extra work I have to put in to get this thing working. You would naturally expect some kind of analogous function. Sleep(), usleep() etc... I scheduled all of 15 seconds for this.

gregj
01-05-2005, 08:42 AM
Two of the three options that come to mind have been mentioned: SDL and pthreads. If you're using C++ you should also consider Boost.Threads. Cross-platform coding can be challanging, especially when it comes to extra-language issues (threading, networking, etc.). OTOH, addressing cross-platform issues carefully can also help build a much more stable program, catching bugs that could crop up as end cases even on your primary platform.