LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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-17-2011, 09:36 AM   #1
apanimesh061
Member
 
Registered: Sep 2010
Posts: 51

Rep: Reputation: 0
How to switch among two threads alternatively ??


Suppose I wish to prints the characters of a string one by one alternatively using only two threads ......
How should I proceed ??

Should I use fork() to divide create a copy of the thread and then pass a new argument to it ???

Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>

void *read_char(void *ptr) {
   char c;
   c = (char) ptr;

   printf("%c", c);
}

int main(void) {
   pthread_t Thread1, Thread2;
   char *str = "This is a string!";
   int len, i;
   len = strlen(str);
   
   for (i=0; i<len; ++i) {
      pthread_create (&thr, NULL, read_char, (void*) str[i]);    
   }
.
.
.
.
I wish the output to be something as ..
Code:
Thread 1: T
Thread 2: h
Thread 1: i
Thread 2: s
Thread 1: 
Thread 2: i
.
.
.
Please help !
 
Old 09-17-2011, 11:04 AM   #2
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,281

Rep: Reputation: 320Reputation: 320Reputation: 320Reputation: 320
You may lock your threads and wait for each other, but that way you just make like one-threaded program. It is some weird for me, can you explain more why you want this, why you need more threads if one is sufficient?
 
Old 09-17-2011, 11:16 PM   #3
bsat
Member
 
Registered: Feb 2009
Posts: 347

Rep: Reputation: 72
If you are doing this just to learn multi-threading then its fine, other wise as pointed out by previous post it surely is not a candidate for multithread programming.
 
Old 09-18-2011, 01:47 PM   #4
apanimesh061
Member
 
Registered: Sep 2010
Posts: 51

Original Poster
Rep: Reputation: 0
Question

I came up with this weird solution ... !
I don't know !

Code:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>

const char* string = "Animesh Pandey";
struct pass {
   char *str;
   int pos;
   int thr_no;
};

void *print1 (void* arg) {
   int n;
   n = (int) arg;

   printf("For Thread1 and %d : %c\n", n, string[n]);

   pthread_exit(NULL);
}

void *print2 (void* arg) {
   int n;
   n = (int) arg;

   printf("For Thread2 and %d : %c\n", n, string[n]);

   pthread_exit(NULL);
}

int main(void) {
   pthread_t thread1, thread2;
   int i;

   for (i=0; i<strlen(string); ++i) {
      if (i%2 == 0) {
         pthread_create (&thread1, NULL, print1, (void*)i);
      }
      else pthread_create (&thread2, NULL, print2, (void*)i);
   }
   
   pthread_join(thread1, NULL);
   pthread_join(thread2, NULL);

   return 0;
}
But the output is never same every time I run this program .... !
The sequence changes every time !
Is there a way I can get the output in the sequence I have mentioned earlier ... ?

Could you please suggest a better way to do this with two threads ... ?

Thanks
 
Old 09-18-2011, 03:38 PM   #5
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.

You do not have direct control on the scheduling the kernel is doing. However, the thing you're aiming on is possible, you just need to use synchronization mechanisms. In pthread library, you can use a mutex, for instance. Read the documentation of:
Code:
int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_unlock(pthread_mutex_t *mutex);
If you have more questions on how to use mutexes, let us know.
 
  


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
Internet comes alternatively ntu929 Linux - Wireless Networking 2 03-20-2011 11:27 PM
Possible to attach sibling threads to parent context-switch? (java) ambius Programming 1 09-05-2009 08:16 PM
Using system calls to create/context switch threads in assembly code casmac Programming 1 07-13-2007 04:48 PM
measuring context switch time between two threads sekhar Programming 1 06-01-2007 07:16 AM

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

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