LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-21-2004, 01:21 PM   #1
slicht
LQ Newbie
 
Registered: Sep 2004
Posts: 2

Rep: Reputation: 0
Unhappy threads won't run concurrently


I can't seem to make my threads run concurrently.

I've posted a simple test program which should print out "Hello World" (along with some debugging messages.)

However, for some reason the main code doesn't advance beyond the 'pthread_create' call until the spawned thread finishes.

As a result, when the thread blocks on the 'pthread_mutex_lock' call (since main() has already locked the mutex), the whole program hangs.

I compile with:
gcc -g thread_test.c -pthread

I'm running a RedHat 7.3
kernel-2.4.18-3
glibc -2.2.5-34

****************************************************
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>

pthread_t test_thread;
pthread_mutex_t test_mutex;
sem_t test_sem;


void *test_thread_function()
{
puts("Thread spawned");
pthread_mutex_lock(&test_mutex);
puts("World");
}

int main()
{
pthread_t test_thread;

pthread_mutex_init(&test_mutex,NULL);
pthread_mutex_lock(&test_mutex);

puts("Ready to spawn thread");
pthread_create(&test_thread, NULL,
test_thread_function(), NULL);

puts("Hello");
pthread_mutex_unlock(&test_mutex);
}
********************************************

Thanks in advance for any help - this is ruining my day...
 
Old 09-21-2004, 11:11 PM   #2
cracauer
Member
 
Registered: Jul 2004
Location: Boston, MA, USA
Distribution: FreeBSD, Debian/AMD64, Ubuntu/i386
Posts: 59

Rep: Reputation: 15
You are accidentially calling the thread's main function

You have:
pthread_create(&test_thread, NULL, test_thread_function(), NULL);

You need:
pthread_create(&test_thread, NULL, test_thread_function, NULL);

You need to pass in the pointer to the function. What you do instead is calling the function and then passing in the return value. Which would usually segfault if it wouldn't deadlock before

Bonus tip: you should use stderr, not stdout to print debug messages in timing-critical situations, because stdout will buffer.

However, be advised that any use of streams will change the timing and locking behavior of the program dramatically - because the I/O will lock the streams and hence synchronise the threads where an i/o free run wouldn't. So you program may work with debug prints but screw up without them.
 
Old 09-22-2004, 09:01 AM   #3
slicht
LQ Newbie
 
Registered: Sep 2004
Posts: 2

Original Poster
Rep: Reputation: 0
Smile

That did the trick - infinite thanks: I was really tearing my hair out here.

(Thanks for the Bonus tip too - I guess that makes infinity + 1)

--S
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Java threads listed using kill -3 does not contain all threads found using ps -auxww coneheed Programming 2 11-14-2005 08:57 AM
c++ threads siemens Programming 4 07-10-2005 11:21 AM
OLD Threads ilnli LQ Suggestions & Feedback 9 12-27-2004 12:30 PM
Java Threads vs Native Threads rjmendez Programming 0 08-16-2004 05:58 AM
How to make two perl functiones work concurrently ???? Bassam Programming 2 03-11-2004 01:56 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration