Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

[C/C++] pthread parallel execution

Name: Anonymous 2007-08-17 21:38 ID:4NoCXfyk

Just started learning to use POSIX threads, but I find that every time I create a thread and/or join it, the calling thread halts until the called thread completes its iteration. How do I execute them parallel to one another?

Thanks.

Name: Anonymous 2007-08-17 21:43 ID:GfZkZ07C

Post your code

Name: 1 2007-08-17 21:50 ID:4NoCXfyk

Example of what I've been trying:

void *CalledFunction(void * i)
{
printf("\nCalledFunction called\n");
//the below was to test for parallelism
while(true)
{
}
//
pthread_exit(NULL);
return 0;
}


void FunctionThatCallsPthreadCreate()
{
int i = 1;
int status;
pthread_create(&threads[i], NULL, CalledFunction, (void *)i);
}

Name: Anonymous 2007-08-17 21:51 ID:cTujiTkz

dont post your code
fucking read a book on WHY NOT TO USE THREADS and your problem will disappear.

Name: 1 2007-08-17 21:53 ID:4NoCXfyk

>>4
Then what do you suppose I should use for parallel programming, hmm?

Name: Anonymous 2007-08-17 21:56 ID:kRXGqyPR

>>1
Uh, pthread_join's purpose is to block until the given thread has finished executing. If you have an infinite loop in the thread, it will block indefinantly.

Name: 1 2007-08-17 21:57 ID:4NoCXfyk

>>6
Note that pthread_join is commented out... I had tried it before, but stopped. It's only using pthread_create at the moment... and still blocking.

Name: Anonymous 2007-08-17 22:05 ID:kRXGqyPR

>>7
I tested your code using the Sun compiler:

$ CC test.cpp -o test -lpthread -mt
"test.cpp", line 23: Warning (Anachronism): Formal argument 3 of type extern "C" void*(*)(void*) in call to pthread_create(unsigned*, const _pthread_attr*, extern "C" void*(*)(void*), void*) is being passed void*(*)(void*).
1 Warning(s) detected.
$ ./test

CalledFunction called
$ jobs
$ time test

real    0m0.000s
user    0m0.000s
sys     0m0.000s
$


Save for the anachronism, it worked fine.

Name: Anonymous 2007-08-17 22:05 ID:CYnAfcBn

>>5
He proposes you should use a 30 years old Unix programming style where processes fork all the time, wasting resources and stealing CPU time.

Name: 1 2007-08-17 22:07 ID:4NoCXfyk

>>8
I'm aware it compiles and runs just fine... but it doesn't execute
parallel to the main thread (and, rather, blocks until thread execution finishes), which is necessary for the application I'm going to need threading for.

Name: 1 2007-08-17 22:09 ID:4NoCXfyk

>>9
I figured as much. Of course, it also needs to be taken into consideration that this program is going to need to run on both nix and Windows... which is exactly why I chose pthreads (considering they are usable on both OS).

Name: Anonymous 2007-08-17 22:10 ID:kRXGqyPR

>>10
I'm not sure I quite understand the problem. How do you know it isn't running parallel to the main thread? There's no reason why it wouldn't. It also shouldn't be blocking, unless you call pthread_join (when the main thread is killed, all child threads spawned will be reaped by OS automatically)

Name: 1 2007-08-17 22:12 ID:4NoCXfyk

>>12
The actual application constantly outputs to the console. I'm not using pthread_join, and when that thread is triggered, all output ceases, and the application doesn't respond to any input (from its sockets). This can only mean that the thread it is executing with pthread_create, which contains the infinite loop specified above specifically for testing if it's running parallel, is blocking the main thread's execution until it completes its iteration.

Name: Anonymous 2007-08-17 22:12 ID:oGsn2Xzf

there is no need to name you "1" though, that's what ids are for.

Name: Anonymous 2007-08-17 22:13 ID:4NoCXfyk

>>14
Habit from another board I visit.

Name: Anonymous 2007-08-17 22:15 ID:kRXGqyPR

>>13
Hrm. I don't know what the problem could be then. Can you post some of your code?

Name: Anonymous 2007-08-17 22:18 ID:4NoCXfyk

>>16
Irrelevant information aside, what I posted above is what it is. The main thread gets a trigger from its socket, calls the function . Function creates the thread that executes the other function.

Name: Anonymous 2007-08-17 22:20 ID:4NoCXfyk

I figured out the problem. It was something that was being executed after thread completion based on status return. Sorry for bothering you all, and thanks for your help and suggestions!

Name: Anonymous 2007-08-17 22:21 ID:kRXGqyPR

I noticed in the pthread_create call, you were referencing some array of pthread_t's to get the output. You sure it isn't a bug there? Other then that, it is a mystery. Most likely some bugs in your code somewhere. Pthread is perfect.

Name: Anonymous 2007-08-17 22:22 ID:Heaven

>>18
Anonymous delivers.

Name: Anonymous 2007-08-17 22:51 ID:cTujiTkz

>>5
Then what do you suppose I should use for parallel programming, hmm?

why the fuck do you want "parallel programming" come to realise there is no difference.

Name: Anonymous 2007-08-17 23:16 ID:oGsn2Xzf

>>5
erlang

Name: Anonymous 2007-08-17 23:18 ID:Heaven

Thread is over. Should be saging.

Name: Anonymous 2007-08-18 0:42 ID:Heaven

ITT >>1 fails at understanding threads. Oh well.

Name: Anonymous 2007-08-18 0:43 ID:Heaven

Indeed, >>1's failure should've been obvious from the thread title -- "C/C++", lol.

Name: Anonymous 2010-03-23 16:33

multiline
quote

Name: Anonymous 2010-12-17 1:34

Erika once told me that Xarn is a bad boyfriend

Name: Anonymous 2010-12-17 1:39

FOLLOW THE NEW GNAA TWITTER AT http://twitter.com/Gary_Niger

Name: Anonymous 2011-02-03 6:23

Don't change these.
Name: Email:
Entire Thread Thread List