Archived

This topic is now archived and is closed to further replies.

krez

multithreading

Recommended Posts

i have been googling for a good hour now, and haven''t found what i need... maybe someone will help me out here does anyone have any good links to tutorials or articles or anything of that sort that will clear up threads for me? everything i found either sucks or is in german (they may or may not suck, i dunno because i don''t understand german). i basically don''t know anything about threads except for the general theory. is there a way to write a multithreaded app that is portable (windows 98, windows 2000, linux)? thanks!

Share this post


Link to post
Share on other sites

The boost organisation are currently working on a specification for threading which should be portable across platforms. At present, you will have to create your own classes from common behaviours.

Share this post


Link to post
Share on other sites
What do you want to know? You should just sit down and play with them to learn them. On Linux look at the pthreads library, and in Windows you can use Win32 functions _beginthreadex() to start a thread. MSDN has a nice section on synchronization objects as well. Writing your own cross platform thread class is pretty easy, but be careful with the design. For instance, Win32 does not let you end a thread from another thread (including the ''main'' thread). I''m not sure pthreads did, but I always assumed that this was the case when I was writing an app and it bit me in the behind.

Share this post


Link to post
Share on other sites
Multithread is quiet simple, here is an example on how it can be done:


  
#include <process.h>

// This is the function that is executed within the thread

// When you end return from the function, the thread is

// end automaticly, or when you call _endthread();

void ThreadFunc(void *pParam)
{
while (true)
{
// Do something here, call _endthread(); to terminate it

}
}

int main()
{
// First parameter is thread function, second stack size which

// will be resized if not big enough, and the last it a void *

// as parameter. You can pass anything to it.

_beginthread(ThreadFunc, 0, NULL);

// Now we have our thread running and we can use both threads

// at the same time. However, if we need to SHARE data between

// the 2 threads, we need to use CriticalSections so the OS

// can block a thread from stopping execution until the data

// is flagged for usage. To start an critical section we use

// this:

CRITICAL_SECTION Cs; // Critical section var, never touch it

// don''t even look at it or copy it


// Init the critical section

InitializeCriticalSection(&Cs);

// Enter critical section

EnterCriticalSection(&Cs);

// In here we can access the shared data. If another thread

// tries to enter critical state, it is blocked until this

// thread releases the critical section like below. Make sure

// you leave the critical sections or your program will lock

// up. So if you need to access shared data in our thread we

// just call EnterCriticalSection and noone can access that

// data.


// Leave critical section

LeaveCriticalSection(&Cs);

// Clean up the critical section

DeleteCriticalSection(&Cs);
}


That''s about it. However, NEVER EVER try to access data that is already access by another thread, because this will crash your program.

Sand Hawk

----------------
-Earth is 98% full. Please delete anybody you can.


My Site

Share this post


Link to post
Share on other sites
quote:
NEVER EVER try to access data that is already access by another thread, because this will crash your program.

Wrong. 100 threads can share a static lookup table without any penalty. The trouble comes when you try to modify that data and two change operations need to lock the data, (so to speak), to work with it, especially if the operation needs more than one instruction in assembly (pretty much everything). Then you use semaphores/mutexes/critical sections. Even if you don''t use them, it won''t crash your program, your data will just be corrupted seemingly randomly.

Share this post


Link to post
Share on other sites
quote:
Original post by Sand_Hawk

// .... However, if we need to SHARE data between
// the 2 threads, we need to use CriticalSections so the OS
// can block a thread from stopping execution until the data
// is flagged for usage. To start an critical section we use
// this:
....


and then
quote:
However, NEVER EVER try to access data that is already access by another thread, because this will crash your program.


well, it seems to be working (thanks folks), but doesn''t this critical section stuff make it so i can safely access the same data?

Share this post


Link to post
Share on other sites