pthreads

Incinerator

Limp Gawd
Joined
Aug 31, 2005
Messages
252
ok, I'm trying to use pthreads to sort an array quickly and I'm using odd-even transposition sort so I can use multiple threads. However, I havn't quite grasped threads yet and I was looking for some help/insight.

now I know with this line pthread_create (&tid, &tattr, start_func, arg); that &tid is filled in if its created correctly, and I'm pretty sure &tattr is too. But I have to add in start_func, so does that mean I should put the name of my swap function there?

and is there any way to make the thread take more than 1 arg? my swap function takes 3, the array, and the 2 indeces that need to be checked regarding whether or not they need to be swapped. Or am I looking at arg completely wrong?
 
Well, you could pass a pointer to a structure, but you should really have an entry function and then do a piece of your sorting in there instead of just trying to call your sorting function directly. (btw the pthread_attr_t is something you set and pass in, just use NULL)

Basic example
Code:
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

void *myThread(void *arg)
{
	// do work
}

int main(void)
{
	pthread_t	tid;
	int		ret;

	ret = pthread_create(&tid, NULL, &myThread, (void *)1);
	if (ret)
	{
		fprintf(stderr, "&#37;d\n", ret);
		return 0;
	}

	ret = pthread_create(&tid, NULL, &myThread, (void *)2);
	if (ret)
	{
		fprintf(stderr, "%d\n", ret);
		return 0;
	}

	printf("Main Thread\n");
	sleep(10);
	return 0;
}

Edit: Although you may want to use pthread_join() instead of sleep() if you really need to wait on them. ;o)
 
Back
Top