unable to use a function to crate a joinable thread


 
Thread Tools Search this Thread
Top Forums Programming unable to use a function to crate a joinable thread
# 1  
Old 04-29-2011
unable to use a function to crate a joinable thread

In my program, threads may be created when some events trigger. So I can't create threads on initialization.

Theremore,I write a createThread() function to create thread. However, it is blocking at first call and don't run anymore? why?

Code:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void *BusyWork(void *t)
{
   int i = 0;
   long tid;
   double result=0.0;
   tid = (long)t;
   printf("Thread %ld starting...\n",tid);
   while ( 1 ) {
      result = result + sin(i) * tan(i);
      i++;
   }
   printf("Thread %ld done. Result = %e\n",tid, result);
   pthread_exit((void*) t);
}


void createThread () {
  int rc;
  void *status;
  pthread_attr_t attr;
  pthread_t thread;
  pthread_attr_init(&attr);
  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
  rc = pthread_create(&thread, &attr, BusyWork, (void *)0); 
  if (rc) {
    printf("ERROR; return code from pthread_create() is %d\n", rc);
    exit(-1);
   }
    pthread_attr_destroy(&attr);
    rc = pthread_join(thread, &status);
    if (rc) {
       printf("ERROR; return code from pthread_join() is %d\n", rc);
       exit(-1);
    }
    printf("Main: completed join with thread %d having a status of %ld\n",0,(long)status);
}

int main (int argc, char *argv[])
{
  createThread();  // <- Blocking here
  createThread();  
  printf("Main: program completed. Exiting.\n");
  pthread_exit(NULL);
}

Thanks for your help!
# 2  
Old 04-29-2011
In your example, you wait for the termination of the BusyWork() thread (using pthread_join) in createThread(). But this thread never terminates because of the while(1) {...}

Hence you get never past the first createThread().

Cheers, Loïc
# 3  
Old 04-29-2011
Quote:
Originally Posted by Loic Domaigne
In your example, you wait for the termination of the BusyWork() thread (using pthread_join) in createThread(). But this thread never terminates because of the while(1) {...}

Hence you get never past the first createThread().

Cheers, Loïc
Hi, when I write the pthread_create() and pthread_join in this way, then the program is OK.

Code:
pthread_create( ... ) <- Thread A
pthread_create( ... ) <- Thread B

pthread_join( ... ) <- Thread A
pthread_join( ... ) <- Thread B

However, I require to design the architecture like
Code:
pthread_create( ... ) <- Thread A
pthread_join( ... ) <- Thread A

In my requirement, once some events or signals come, then a thread is created for handle it; if no events then no thread is created.
# 4  
Old 04-29-2011
the thread calling pthread_join() blocks until the targeted thread either returns from the start routine or calls pthread_exit().

That's it. It is difficult for us to help you further (the reason why your example didn't worked is clear - but doesn't seem to reflect your real problem?).

Cheers, Loïc
# 5  
Old 04-29-2011
Quote:
Originally Posted by sehang
Hi, when I write the pthread_create() and pthread_join in this way, then the program is OK.

Code:
pthread_create( ... ) <- Thread A
pthread_create( ... ) <- Thread B

pthread_join( ... ) <- Thread A
pthread_join( ... ) <- Thread B

However, I require to design the architecture like
Code:
pthread_create( ... ) <- Thread A
pthread_join( ... ) <- Thread A

In my requirement, once some events or signals come, then a thread is created for handle it; if no events then no thread is created.
There's no reason to start a separate thread if all you're going to do is call pthread_join() and wait for it to finish.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

Unable to capture value from function

Hi Experts, Am writing a code which need to check for the previous day date and pickup the file as per the previous day date. Problem: Why variable "YDATE" is empty ? O/S: RHEL 5.6 Shell: BASH Desired O/P: ls -lrt /opt/test/user/atsuser.NHU/out/demon.08272017 When I checked the... (3 Replies)
Discussion started by: pradeep84in
3 Replies

2. Forum Support Area for Unregistered Users & Account Problems

Unable to create new thread

Hi. I registered, but when trying to post, I get ... "Sorry, you do not have enough Bits to post a new thread" Google & Forum search showed some hits, but no solution. Please advide. Thanks! (1 Reply)
Discussion started by: gehe
1 Replies

3. Forum Support Area for Unregistered Users & Account Problems

Unable to post or reply thread after login

I am unable to post reply to a thread. Whenever I will reply to any post I get the message every time : The message you have entered is too short. Please lengthen your message to at least 5 characters. Even though length of the message is more than required. for example I tried to post ... (3 Replies)
Discussion started by: Unregistered
3 Replies

4. Shell Programming and Scripting

Deliver values to Thread function

Hello, I hope you can help me with my problem. I've written a programm which creates a thread. The main programmm creates 10 int and float values. The fucnction should give the values out. May big problem ist that the delivery to the function is not working. Where ist my fault? How can I make... (1 Reply)
Discussion started by: jimm_the_h
1 Replies

5. Programming

Thread function local variables

As I know threads share the memory. But, what about the local variables in the thread function? if i call multiple threads would they allocate seperate local variables for themselves? like thread_func() { int i, j; string... } Are the above local variables defined for each of... (1 Reply)
Discussion started by: saman_glorious
1 Replies

6. Programming

Unable to create a UDP client from thread?

I try to initial a UDP client from threading, but it doesn't work? why? These codes from the textbook #define ECHOMAX 255 /* Longest string to echo */ #define TIMEOUT_SECS 2 /* Seconds between retransmits */ #define MAXTRIES 5 /* Tries before giving up */... (3 Replies)
Discussion started by: sehang
3 Replies

7. Forum Support Area for Unregistered Users & Account Problems

unable to post thread

Hello, I'd like to post a aix thread but I'm unable to. Please let me know why this is happening. Thanks.... (1 Reply)
Discussion started by: sullivjp
1 Replies

8. Programming

create a thread from a returning function

hi all, my requirement is to create a thread by calling another function. i.e i dont call pthread_create directly from main, but by calling another function (createThd - below ), from main. Example: void *thread_function(void *arg) { /* thread function */ int i; rc =... (3 Replies)
Discussion started by: wolwy_pete
3 Replies

9. Post Here to Contact Site Administrators and Moderators

Unable to open thread

I am unable to open the mimesender thread. I get the following message when clicking on https://www.unix.com/showthread.php?p=33233#post33233 : (2 Replies)
Discussion started by: dangral
2 Replies

10. HP-UX

create thread C with JNI function with JAVA

Hello, J create a thread C with a JNI function via JAVA. J have the following message (but not in each time): Someone has an idea ? Thank. Unexpected Signal : 4 occurred at PC=0x78C103E0 Function= Library=(N/A) NOTE: We are unable to locate the function name... (0 Replies)
Discussion started by: AUBERT
0 Replies
Login or Register to Ask a Question