LinuxQuestions.org
Visit Jeremy's Blog.
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 12-03-2004, 05:49 AM   #1
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Rep: Reputation: 50
About POSIX threads


I am learning about threads in POSIX. I can create a thread that runs a function. But my goal is to have a lot of threads that each has it's own ID-variable that it can use (this is the reason why I chosed threads, it was between threads and forks).

But now the problem is that each thread waits for the other to run. I tried to remove "pthread_join()" but then only one thread ran. Here is the code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void *t_func(void*);
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;

int main() {
        pthread_t th1, th2;
        int iret1, iret2;

        iret1 = pthread_create(&th1, NULL, t_func, (void*)100);
        iret2 = pthread_create(&th2, NULL, t_func, (void*)3);

        pthread_join(th1, NULL);
        pthread_join(th2, NULL);

        printf("done\n");

        exit(0);
}

void *t_func(void *tmp) {
        int n = (int)tmp;
        printf("#%d starting\n", n);
        printf("#%d ending\n", n);
}
I want the two threads to run at the same time, so the output would be:
Code:
#100 starting
#3 starting
#100 ending
#3 ending
done
As I said, now the threads run after each other (starting, ending the first, starting, ending the second). And when I removed "pthread_join" I got "starting, ending the first" and then the program stopped. The second thread didn't run.
 
Old 12-03-2004, 06:33 AM   #2
Marius2
Member
 
Registered: Jan 2004
Location: Munich
Distribution: SuSE 9.2, 10.2, 10.3, knoppix
Posts: 276

Rep: Reputation: 31
The second thread doesn't wait for the first. It's just that thread 1 doesn't exist
long enough to be still there when thread 2 is created. Modify your thread function
like this:

void *t_func(void *tmp) {
int n = (int)tmp;
int c=0;
printf("#%d starting\n", n);
while(c++<10000){
printf(#%d doing something\n");
usleep(1000*50);
}
printf("#%d ending\n", n);
}


HTH
 
  


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
POSIX threads - pthread_cond_timedwait iklinux Programming 5 09-26-2005 10:33 AM
Signal Handling in Threads[POSIX] anoop_cv Programming 1 11-02-2004 02:00 AM
enable pthreads? (posix threads) cpluspaul Slackware 5 06-29-2004 05:56 AM
a ./configure doesnt see any working posix threads RokOne Linux - Software 0 04-06-2004 08:06 PM
I have a problem with POSIX Threads. AndreasA Programming 2 08-11-2003 02:48 PM

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

All times are GMT -5. The time now is 10:56 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