LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 03-04-2010, 03:40 PM   #1
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Multithreaded app using pth does not work


I threw this togher based on the examples but the thread does not seem to run:
Code:
#include <stdio.h>
#include <pth.h>
static void *thread(void *arg){
 while(TRUE){
  printf("Thread\n");
 }
}
int main(){
 signal(SIGPIPE, SIG_IGN);
 long tick = 0;
 pth_attr_t attr;
 if(pth_init() == FALSE){printf("CANNOT INIT PTH");}
 attr = pth_attr_new();
 pth_attr_set(attr, PTH_ATTR_NAME, "thread");
 pth_attr_set(attr, PTH_ATTR_STACK_SIZE, 4096);
 pth_attr_set(attr, PTH_ATTR_JOINABLE, FALSE);
 if(pth_spawn(attr, thread, NULL) == NULL){printf("ERROR!");}
 while(tick < 65536){
  tick++;
  //fprintf(stderr, "Main\n");
 }
return 0;
}
It basically runs for ~750 ms and then terminates without a trace.
 
Old 03-04-2010, 05:54 PM   #2
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
First, I've never used or heard of this library until now. A quick search brings up
Quote:
Originally Posted by http://www.gnu.org/software/pth/pth-manual.html
In non-preemptive scheduling, once a thread received control from the scheduler it keeps it until either a blocking situation occurs (again a function call which would block and instead switches back to the scheduler) or the thread explicitly yields control back to the scheduler in a cooperative way.
When you create a thread using this library, that new thread doesn't have the chance to execute until one of the above mentioned events occur. In your program you create the thread but never let it execute. When you want it to execute, you can use "pth_yield", passing the "pth_t" returned by "pth_spawn" to let it execute.

Of course if you do this, then the program won't end due to the new thread being in an infinite loop. So you'll need to use signals or perform some blocking operation to let have the other thread ("main") continue. Also, you've declared "thread" to return "void*" but you don't return anything.

Last edited by nadroj; 03-04-2010 at 06:00 PM.
 
1 members found this post helpful.
Old 03-04-2010, 06:01 PM   #3
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by smeezekitty View Post
It basically runs for ~750 ms and then terminates without a trace.
Code:
    while(tick < 65536) {
        tick++;
        // Put a call to sleep here
        sleep(1);
    }
Put a call to sleep in the location shown above.
 
1 members found this post helpful.
Old 03-04-2010, 06:08 PM   #4
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Original Poster
Rep: Reputation: 231Reputation: 231Reputation: 231
Ok now its working, i put this line in both the main and threaded loop.
Code:
pth_nap(pth_time(0, 500));
 
  


Reply



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
LXer: Work the APP in Deployed Applications LXer Syndicated Linux News 0 11-14-2006 08:54 AM
wget multithreaded AlexiaKeth Linux - Software 1 06-25-2006 04:03 PM
Is there any app which can resize ntfs & which can work without Linux install? kornerr Linux - General 7 05-13-2005 01:49 AM
gnu/pth api ltcstyle Programming 0 12-04-2004 12:12 PM
ARG how do I get this perl app to work? rivethead Linux - Software 4 02-03-2003 06:18 PM

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

All times are GMT -5. The time now is 10:41 PM.

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