extern void * threadrunner( void *parms );
pthread_t tid[10];
for( int i = 0; i < 10; ++i )
pthread_create( &tid[i], NULL, threadrunner, NULL );
for( int i = 0; i < 10; ++i )
pthread_join( tid[i], NULL );
// threadrunner defn
void * threadrunner( void *parms )
{
sleep(20);
}
So now, each thread will sleep for 20 seconds before they can join into the main 'thread'. If you run this in background and run a ps -u
Recently there has been a new POSIX thread library that patches the kernel to modify the main process structure within the kernel code.
struct task_struct;
It adds a 'tid' (thread id), 'tgid' (thread group id) to the struct. It also modifies the getpid and the getgid calls to return the tid and the tgid respectively whenever the process id is the same. This implementation is a lot better than the previous one. I guess that it's supported only on the newer 2.6.x kernels.
So, all this while, were we threading processes or processing threads?! :-)
No comments:
Post a comment
What I want to say is: