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:
Anonymous2007-08-17 21:43 ID:GfZkZ07C
Post your code
Name:
12007-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:
Anonymous2007-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:
12007-08-17 21:53 ID:4NoCXfyk
>>4
Then what do you suppose I should use for parallel programming, hmm?
Name:
Anonymous2007-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:
12007-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:
Anonymous2007-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:
Anonymous2007-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:
12007-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:
12007-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:
Anonymous2007-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:
12007-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:
Anonymous2007-08-17 22:12 ID:oGsn2Xzf
there is no need to name you "1" though, that's what ids are for.
>>13
Hrm. I don't know what the problem could be then. Can you post some of your code?
Name:
Anonymous2007-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:
Anonymous2007-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:
Anonymous2007-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.