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 09-07-2004, 11:11 AM   #1
indian
Member
 
Registered: Aug 2004
Posts: 137

Rep: Reputation: 15
how to write multiple threads working simultaneously ?


Hi , How to write multiple threads in a C program ?...and I think pthread_join will join two threads so that when one will be completed than the other will be started ...but what I want is that first one thread shud work than second thread shud work and than again it gives control to the first thread....i.e. the control shud get transfer without killing any of the threads...I hope I am clear....Thnaking you..
 
Old 09-08-2004, 09:04 AM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
You can't control the threads directly (tell the system: now run that thread). But you can have nearly the same result when you use mutexes or semaphores. Semaphores (in general, mutexes can be threated like semaphores) allow only a certain number of threads to run. For example, if a semaphore allows only 3 processes to go, when the fourth one tries to pass, it's stopped until one of the three releases the semaphore. I'm not trying to explain semaphores here, because books are written about them.

In your situation, the following scheme should be Ok:
Code:
thread_1        thread_2        thread_3
running

m1.up()
completed     running
              m2.up()
              completed
                                running
The initial state is that m1 and m2 are set to 0 (a thread must wait until the up() operation is performed).

Last edited by Mara; 09-08-2004 at 09:05 AM.
 
Old 09-15-2011, 12:59 AM   #3
vamsi9042
LQ Newbie
 
Registered: Sep 2011
Location: Bangalore
Distribution: Ubuntu
Posts: 22

Rep: Reputation: Disabled
use semaphores

Quote:
Originally Posted by indian View Post
Hi , How to write multiple threads in a C program ?...and I think pthread_join will join two threads so that when one will be completed than the other will be started ...but what I want is that first one thread shud work than second thread shud work and than again it gives control to the first thread....i.e. the control shud get transfer without killing any of the threads...I hope I am clear....Thnaking you..
hi, use semaphores or mutexes(if only two threads) between threads.
system calls are sem_wait and sem_post. for more details see man pages.
 
1 members found this post helpful.
Old 09-20-2011, 12:41 AM   #4
vamsi9042
LQ Newbie
 
Registered: Sep 2011
Location: Bangalore
Distribution: Ubuntu
Posts: 22

Rep: Reputation: Disabled
Smile

Quote:
Originally Posted by vamsi9042 View Post
hi, use semaphores or mutexes(if only two threads) between threads.
system calls are sem_wait and sem_post. for more details see man pages.
Hi if u think that this problem is solved, please mark the thread as solved.
Thanks.
 
  


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
Multiple threads vs Multiple processes deveraux83 Programming 1 05-11-2005 11:22 AM
Multiple Threads excel28 Programming 5 01-06-2004 09:46 PM
Getting glidepoint and mouse working simultaneously cozye Slackware 5 11-24-2003 09:13 AM
USB mouse + touchpad working simultaneously stevenhasty Slackware 1 07-26-2003 08:11 AM
how to write a program can have 2 threads running at the same time?? man9 Programming 3 10-07-2000 02:43 PM

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

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